Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   template, cast, and operator (http://www.velocityreviews.com/forums/t956263-template-cast-and-operator.html)

thomas.grund@mail.com 01-07-2013 06:29 PM

template, cast, and operator
 
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
}

Zhihao Yuan 01-07-2013 06:47 PM

Re: template, cast, and operator
 
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/

Victor Bazarov 01-07-2013 06:49 PM

Re: template, cast, and operator
 
On 1/7/2013 1:29 PM, thomas.grund@mail.com 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

Victor Bazarov 01-07-2013 06:51 PM

Re: template, cast, and operator
 
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

thomas.grund@mail.com 01-07-2013 07:01 PM

Re: template, cast, and operator
 
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

Zhihao Yuan 01-07-2013 07:46 PM

Re: template, cast, and operator
 
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/

thomas.grund@mail.com 01-08-2013 10:17 AM

Re: template, cast, and operator
 
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

88888 Dihedral 01-08-2013 11:50 AM

Re: template, cast, and operator
 
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
>
> }



88888 Dihedral 01-09-2013 10:34 AM

Re: template, cast, and operator
 
在 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.


All times are GMT. The time now is 10:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


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