user defined conversion operator or operator overloading?

Discussion in 'C++' started by hurcan solter, Aug 29, 2007.

  1. 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....
     
    hurcan solter, Aug 29, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Aug 29, 2007
    #2
    1. Advertising


  3. > 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.
    > >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 ...
     
    hurcan solter, Aug 29, 2007
    #3
  4. hurcan solter

    Cholo Lennon Guest

    On Aug 29, 3:31 pm, hurcan solter <> wrote:
    > > 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.
    >
    > > >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_typetraits/reference.html#boost_typetraits.is_convertible


    Regards

    --
    Cholo Lennon
    Bs.As.
    ARG
     
    Cholo Lennon, Aug 29, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip Pemberton
    Replies:
    5
    Views:
    579
    DaKoadMunky
    Oct 24, 2004
  2. Replies:
    0
    Views:
    389
  3. 2simple
    Replies:
    1
    Views:
    2,153
    2simple
    Oct 19, 2006
  4. Oodini
    Replies:
    1
    Views:
    1,838
    Keith Thompson
    Sep 27, 2005
  5. Randy
    Replies:
    27
    Views:
    820
    Randy
    Jan 5, 2006
Loading...

Share This Page