Conversion constructors and template arguments.

Discussion in 'C++' started by Kevin Ruland, Feb 3, 2005.

  1. Kevin Ruland

    Kevin Ruland Guest

    Hi all.

    I have a template class with conversion operator:

    template< typename T >
    class FooWrapper {
    public:
    FooWrapper( const T& rhs );
    }

    Along with some specializations for this:

    template<>
    class FooWrapper<MyType> {
    public:
    FooWrapper( const MyType& rhs );
    }

    and so forth.

    Now I'm defining some operators using templates which I want to be
    limited to only FooWrapper<T> classes.

    template<typename T, typename U>
    int
    operator+ (const FooWrapper<T>&, const FooWrapper<T>& );


    The problem I'm having is compiling code like this:

    MyType a, b;
    int i = a + b;

    Under g++ 3.2.3.20030502 it states there is no operator+ for MyType&,
    MyType&.

    Really, my question is why are the user defined conversions not
    considered when trying to match with the customer operator+?

    Thanks much.

    Kevin Ruland
    Kevin Ruland, Feb 3, 2005
    #1
    1. Advertising

  2. "Kevin Ruland" <> wrote...
    > [...]
    > Really, my question is why are the user defined conversions not considered
    > when trying to match with the customer operator+?


    Just a thought: the "why" questions should really be asked in
    comp.std.c++. That's where they discuss the rationale behind
    certain decisions that control how C++ is standardized.

    V
    Victor Bazarov, Feb 3, 2005
    #2
    1. Advertising

  3. Kevin Ruland

    Shezan Baig Guest

    Kevin Ruland wrote:
    > I have a template class with conversion operator:
    >
    > template< typename T >
    > class FooWrapper {
    > public:
    > FooWrapper( const T& rhs );
    > }
    >
    > Along with some specializations for this:
    >
    > template<>
    > class FooWrapper<MyType> {
    > public:
    > FooWrapper( const MyType& rhs );
    > }
    >

    [snip]
    >
    > template<typename T, typename U>
    > int
    > operator+ (const FooWrapper<T>&, const FooWrapper<T>& );
    >

    [snip]
    >
    > MyType a, b;
    > int i = a + b;
    >
    > Under g++ 3.2.3.20030502 it states there is no operator+ for MyType&,


    > MyType&.
    >
    > Really, my question is why are the user defined conversions not
    > considered when trying to match with the customer operator+?


    I'm not sure if this will work, but how about if you instantiate
    operator+ for T = MyType:

    template int operator+(const FooWrapper<MyType>&, const
    FooWrapper<MyType>&);

    Hope this helps,
    -shez-
    Shezan Baig, Feb 3, 2005
    #3
  4. Kevin Ruland

    Kevin Ruland Guest

    Shezan Baig wrote:
    > Kevin Ruland wrote:
    >
    >>I have a template class with conversion operator:
    >>
    >>template< typename T >
    >>class FooWrapper {
    >>public:
    >> FooWrapper( const T& rhs );
    >>}
    >>
    >>Along with some specializations for this:
    >>
    >>template<>
    >>class FooWrapper<MyType> {
    >>public:
    >> FooWrapper( const MyType& rhs );
    >>}
    >>

    >
    > [snip]
    >
    >>template<typename T, typename U>
    >>int
    >>operator+ (const FooWrapper<T>&, const FooWrapper<T>& );
    >>

    >
    > [snip]
    >
    >>MyType a, b;
    >>int i = a + b;
    >>
    >>Under g++ 3.2.3.20030502 it states there is no operator+ for MyType&,

    >
    >
    >>MyType&.
    >>
    >>Really, my question is why are the user defined conversions not
    >>considered when trying to match with the customer operator+?

    >
    >
    > I'm not sure if this will work, but how about if you instantiate
    > operator+ for T = MyType:
    >
    > template int operator+(const FooWrapper<MyType>&, const
    > FooWrapper<MyType>&);
    >

    Actually this doesn't help. It still doesnt find the conversion operator.

    I also tried adding an implicit conversion operator to MyClass, and that
    didn't fix it.

    If I were to do this, I might as well, not implement the operator as a
    template to begin with and instead just implement all the combinations
    that I'm going to need. The problem is I really have 4 different
    instantiations of FooWrapper, so I end up with 16 different operator+.
    And of course, I also have operator-, *, / etc....

    Thanks for the advice though.

    > Hope this helps,
    > -shez-
    >
    Kevin Ruland, Feb 3, 2005
    #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. Dave Rudolf
    Replies:
    12
    Views:
    8,245
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    572
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    582
    Ron Natalie
    Jun 7, 2007
  4. Replies:
    1
    Views:
    2,081
    Gianni Mariani
    Jun 8, 2007
  5. Peng Yu
    Replies:
    5
    Views:
    381
    Juha Nieminen
    Sep 19, 2008
Loading...

Share This Page