468,456 Members | 1,688 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

Conversion constructors and template arguments.


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
Jul 23 '05 #1
3 1211
"Kevin Ruland" <kr*****@ku.edu> 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
Jul 23 '05 #2

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-

Jul 23 '05 #3
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-

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by bartek | last post: by
14 posts views Thread by ES Kim | last post: by
9 posts views Thread by Tanmoy Bhattacharya | last post: by
5 posts views Thread by Vijai Kalyan | last post: by
2 posts views Thread by Clark S. Cox III | last post: by
15 posts views Thread by Alexander Stippler | last post: by
2 posts views Thread by coder_lol | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.