user defined conversion operator or operator overloading?

H

hurcan solter

I have an host class that holds fundamental types

template<typename T>
struct Generic{

Generic(T val= T()):mval(val){}
operator T(){return mval;)
T mval;
}

template<typename T1,typename T2>
Generic<T1 or T2 ???>operator+(const Generic<T1>& lhs,const
Generic<T2>& rhs)
{
return Generic<T1 or T2 ???>(lhs.mval+rhs.mval);
}

Is there a compelling reason to discard the conversion operator and
define overloaded arithmetic operators because they are considered
dangerous?I'd like that class to behave like fundamental types.but i
dont want to overload every operator over there . what are the issues
i should be aware of if i stick with the user defined conversion
operator?

thanks a lot
hurcan....
 
V

Victor Bazarov

hurcan said:
I have an host class that holds fundamental types

template<typename T>
struct Generic{

Generic(T val= T()):mval(val){}
operator T(){return mval;)
T mval;
}

template<typename T1,typename T2>
Generic<T1 or T2 ???>operator+(const Generic<T1>& lhs,const
Generic<T2>& rhs)
{
return Generic<T1 or T2 ???>(lhs.mval+rhs.mval);


The presense of the question marks seems to indicate that you do
not know what to use here. You essentially need a helper class
that would define the type of the addition of T1 and T2. You could
hard-code those, or see if Boost folks have already come up with
something.
}

Is there a compelling reason to discard the conversion operator and
define overloaded arithmetic operators because they are considered
dangerous?

Cosidered dangerous by whom?
I'd like that class to behave like fundamental types.but i
dont want to overload every operator over there . what are the issues
i should be aware of if i stick with the user defined conversion
operator?

To be honest with your, I am not even sure why you'd need such
a type like your 'Generic'. What purpose would is serve?

V
 
H

hurcan solter

The presense of the question marks seems to indicate that you do
not know what to use here. You essentially need a helper class
that would define the type of the addition of T1 and T2. You could
hard-code those, or see if Boost folks have already come up with
something.
it means I am unable to determine whether T1 can be promoted to T2
or vice versa
so i can return the correct Generic<T> without loss of
information.if they were fundamental types compiler would handle it
for me...
Cosidered dangerous by whom?

same people who invented std::string.c_str() ? Or (a long shot but)
the need for explicit keyword.
To be honest with your, I am not even sure why you'd need such
a type like your 'Generic'. What purpose would is serve?

it was for demonstrating the gist of the problem(the real name isn't
even Generic), In reality it has some policies can be configured into
it and some other methods.

hurcan ...
 
C

Cholo Lennon

it means I am unable to determine whether T1 can be promoted to T2
or vice versa
so i can return the correct Generic<T> without loss of
information.if they were fundamental types compiler would handle it
for me...


same people who invented std::string.c_str() ? Or (a long shot but)
the need for explicit keyword.



it was for demonstrating the gist of the problem(the real name isn't
even Generic), In reality it has some policies can be configured into
it and some other methods.

hurcan ...

Take a look to boost::is_convertible for a possible implementation

http://www.boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.is_convertible


Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top