473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

user defined conversion operator or operator overloading?

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....

Aug 29 '07 #1
3 2140
hurcan solter wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 29 '07 #2
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<Twithout 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.
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?
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 ...
Aug 29 '07 #3
On Aug 29, 3:31 pm, hurcan solter <hsol...@gmail.comwrote:
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<Twithout 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.
>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?

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_...is_convertible
Regards

--
Cholo Lennon
Bs.As.
ARG
Aug 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: uvts_cvs | last post by:
Hi to everybody, can someone help me? This compiles in C++: struct Foo { // Function template bar template <class T> void bar (T t){} // Function bar that forwards to bar<T>
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
3
by: bsa | last post by:
Hi, This maybe a stupid question for which I apologies, but I couldn't find a solutions so I'll ask. I'm a newbie and I'm trying to do the following: A have a base class called space that...
0
by: Aaron Queenan | last post by:
ECMA 334 section 13.4.3 User-defined implicit conversions states that D, the set of types from which user-defined conversion operators will be considered, includes (among other things) the base...
1
by: Anna | last post by:
I have two data types in my program: Foo1 and FooTD (which is a typedef of type Foo2) Foo1 has an operator= function which takes a const& to Foo2 as input and assigns it to a Foo1 type. ...
3
by: Belebele | last post by:
Suppose that I have a class that has a user-defined conversion to std::string, and I would like to compare objects of that class to "strings" (e.g. literals, std::strings): class InUpperCase {...
1
by: John Lampe | last post by:
Hi all, I've got a very simple "container" class that encapsulates an (presumably opaque) OS handle, which is typedef'd in the OS headers to be equivalent to an unsigned int (I'm simplifying a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.