Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template, cast, and operator

Reply
Thread Tools

template, cast, and operator

 
 
thomas.grund@mail.com
Guest
Posts: n/a
 
      01-07-2013
Hello,

the following does not compile due to problems with the + operator. The same thing works without templates. How can I achive this?

Thanks a lot,

Thomas


template <typename T = double> class CVariable {
};

template <typename T = double> class CSymbolic {
public:
CSymbolic(CVariable<T>){}
CSymbolic(){}
};

template <typename T> CSymbolic<T> operator+(CSymbolic<T> a1, CSymbolic<T> a2) {
return CSymbolic();
}

int main() {
CVariable<> x, y;
CSymbolic<> f1 = x+y; // problem here
}
 
Reply With Quote
 
 
 
 
Zhihao Yuan
Guest
Posts: n/a
 
      01-07-2013
On Monday, January 7, 2013 12:29:58 PM UTC-6, thomas...@mail.com wrote:
> template <typename T> CSymbolic<T> operator+(CSymbolic<T> a1, CSymbolic<T> a2) {
> return CSymbolic();
> }


Since CVariable<> is not convertible to CSymbolic, this overload is
not selected. Make it convertible (you may need a forward
declaration), or have one more overload.

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
__________________________________________________ _
4BSD -- http://4bsd.biz/
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-07-2013
On 1/7/2013 1:29 PM, wrote:
> the following does not compile due to problems with the + operator. The same thing works without templates. How can I achive this?
>
> Thanks a lot,
>
> Thomas
>
>
> template <typename T = double> class CVariable {
> };
>
> template <typename T = double> class CSymbolic {
> public:
> CSymbolic(CVariable<T>){}
> CSymbolic(){}
> };
>
> template <typename T> CSymbolic<T> operator+(CSymbolic<T> a1, CSymbolic<T> a2) {
> return CSymbolic();
> }
>
> int main() {
> CVariable<> x, y;
> CSymbolic<> f1 = x+y; // problem here
> }


What are you trying to achieve? The problem is simple: there is no
operator+ defined for operands of type CVariable<double>. The compiler
is unable to use the operator+ that you defined for CSymbolic<> because
when trying to figure out the type T to instantiate your operator+
*template*, it cannot (shall not) consider conversions.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-07-2013
On 1/7/2013 1:47 PM, Zhihao Yuan wrote:
> On Monday, January 7, 2013 12:29:58 PM UTC-6, thomas...@mail.com wrote:
>> template <typename T> CSymbolic<T> operator+(CSymbolic<T> a1, CSymbolic<T> a2) {
>> return CSymbolic();
>> }

>
> Since CVariable<> is not convertible to CSymbolic,


Uh... But it *is* convertible. Please look again, CSymbolic has a
c-tor that takes CVariable as an argument.

> this overload is
> not selected. Make it convertible (you may need a forward
> declaration), or have one more overload.
>


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
thomas.grund@mail.com
Guest
Posts: n/a
 
      01-07-2013
On Monday, January 7, 2013 7:49:23 PM UTC+1, Victor Bazarov wrote:
> On 1/7/2013 1:29 PM, Thomas Grund wrote:
>
> > the following does not compile due to problems with the + operator. Thesame thing works without templates. How can I achive this?

>
> >

>
> > Thanks a lot,

>
> >

>
> > Thomas

>
> >

>
> >

>
> > template <typename T = double> class CVariable {

>
> > };

>
> >

>
> > template <typename T = double> class CSymbolic {

>
> > public:

>
> > CSymbolic(CVariable<T>){}

>
> > CSymbolic(){}

>
> > };

>
> >

>
> > template <typename T> CSymbolic<T> operator+(CSymbolic<T> a1, CSymbolic<T> a2) {

>
> > return CSymbolic();

>
> > }

>
> >

>
> > int main() {

>
> > CVariable<> x, y;

>
> > CSymbolic<> f1 = x+y; // problem here

>
> > }

>
>
>
> What are you trying to achieve? The problem is simple: there is no
>
> operator+ defined for operands of type CVariable<double>. The compiler
>
> is unable to use the operator+ that you defined for CSymbolic<> because
>
> when trying to figure out the type T to instantiate your operator+
>
> *template*, it cannot (shall not) consider conversions.
>
>
>
> V
>
> --
>
> I do not respond to top-posted replies, please don't ask


I am playing around with symbolic calculus (see ginac-library). There is a typical structure of a base class from which several things are derived as sum, product, variable and so on. Then there is a wrapper (CSymbolic) whichwrapps all different types (contains pointer to base class). Everything works if the underlying numerical type is fixed (e.g. double). I want to makethis numerical type to be defined by the user to allow for instance interval arithmetic (interval type instead of double). Hope I made things clear.

Thanks for solutions!

Thomas
 
Reply With Quote
 
Zhihao Yuan
Guest
Posts: n/a
 
      01-07-2013
On Monday, January 7, 2013 12:51:34 PM UTC-6, Victor Bazarov wrote:
> Uh... But it *is* convertible. Please look again, CSymbolic has a
> c-tor that takes CVariable as an argument.


Oops, sorry.

The problem is caused by function template deducution, conversion is
not considered here.

Enable it, in a quick and dirty way:

template <template <typename> class C, typename T,
typename = typename std::enable_if<
std::is_convertible<C<T>, CSymbolic<T>>::value>::type>
CSymbolic<T> operator+(C<T> a1, C<T> a2) {
return {};
}

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
__________________________________________________ _
4BSD -- http://4bsd.biz/
 
Reply With Quote
 
thomas.grund@mail.com
Guest
Posts: n/a
 
      01-08-2013
On Monday, January 7, 2013 8:46:19 PM UTC+1, Zhihao Yuan wrote:
> On Monday, January 7, 2013 12:51:34 PM UTC-6, Victor Bazarov wrote: > Uh.... But it *is* convertible. Please look again, CSymbolic has a > c-tor thattakes CVariable as an argument. Oops, sorry. The problem is caused by function template deducution, conversion is not considered here. Enable it, in a quick and dirty way: template <template <typename> class C, typename T, typename = typename std::enable_if< std::is_convertible<C<T>, CSymbolic<T>>::value>::type> CSymbolic<T> operator+(C<T> a1, C<T> a2) { return {}; } --Zhihao Yuan, ID lichray The best way to predict the future is to invent it.. __________________________________________________ _ 4BSD -- http://4bsd.biz/


I have to learn this technique and will give it a try.

Thank you very much!

Thomas
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      01-08-2013
On Tuesday, January 8, 2013 2:29:58 AM UTC+8, thomas...@mail.com wrote:
> Hello,
>
>
>
> the following does not compile due to problems with the + operator. The same thing works without templates. How can I achive this?
>
>
>
> Thanks a lot,
>
>
>
> Thomas
>
>
>
>
>
> template <typename T = double> class CVariable {
>
> };
>
>
>
> template <typename T = double> class CSymbolic {
>
> public:
>
> CSymbolic(CVariable<T>){}
>
> CSymbolic(){}
>
> };
>
>
>
> template <typename T> CSymbolic<T> operator+(CSymbolic<T> a1, CSymbolic<T> a2) {
>
> return CSymbolic();
>
> }
>
>
>
> int main() {
>
> CVariable<> x, y;
>


OBJECT a= OBJECT b by the copy constructor first.

Then it is trivial that
a+=c for the object modifier +=.


If the object size of b can't copy to a,
pray for the old code can be morphed to use
objects in the derived classes.




> CSymbolic<> f1 = x+y; // problem here
>
> }


 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      01-09-2013
在 2013年1月9日星期三UTC+8上午11时25分14秒 ,Dan Stewart写道:
> On Mon, 07 Jan 2013 10:29:58 -0800, thomas.grund wrote:
>
>
>
> > Hello,

>
> >

>
> > the following does not compile due to problems with the + operator. The

>
> > same thing works without templates. How can I achive this?

>
> >

>
> > Thanks a lot,

>
> >

>
> > Thomas

>
> >

>
> >

>
> > template <typename T = double> class CVariable {

>
> > };

>
> >

>
> > template <typename T = double> class CSymbolic {

>
> > public:

>
> > CSymbolic(CVariable<T>){} CSymbolic(){}

>
> > };

>
> >

>
> > template <typename T> CSymbolic<T> operator+(CSymbolic<T> a1,

>
> > CSymbolic<T> a2) {

>
> > return CSymbolic();

>
> > }

>
> >

>
> > int main() {

>
> > CVariable<> x, y;

>
> > CSymbolic<> f1 = x+y; // problem here

>
> > }

>
>
>
> In this case when you call x + y you are trying to instantiate the
>
> function
>
>
>
> CSymbolic<double> operator+(CSymbolic<double> a1, CSymbolic<double> b1)
>
>
>
> with template argument CVariable<double>
>
>
>
> However, there are only 2 cases where conversion from a template argument
>
> to a template parameter is allowed:
>
>
>
> a) Template argument is a reference/pointer to T and template parameter
>
> is a reference/pointer to const T
>
>
>
> or
>
>
>
> b) Template argument is an array or function, in which case the template
>
> parameter is deduced to a pointer to the array or function type
>
>
>
> In this case you are trying instantiate the above function using a user-
>
> defined conversion from CVariable<double> to CSymbolic<double>, which
>
> templates won't allow, since the intended conversion doesn't fall under
>
> a) or b). Hope this helps.

I am thinking there is no closure concept in C++
operator reloading.

Anyway, for fixed types languages, I don't think
it is too difficult for various types.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POD and assignment operator and test operator Hicham Mouline C++ 2 09-01-2009 06:00 PM
Allocation with new operator and Destroying with free operator saikishore.vanga@wipro.com C++ 8 12-22-2005 01:35 PM
Member operators operator>>() and operator<<() Alex Vinokur C++ 3 03-20-2005 03:11 PM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57