Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Conversion constructors and template arguments.

Reply
Thread Tools

Conversion constructors and template arguments.

 
 
Kevin Ruland
Guest
Posts: n/a
 
      02-03-2005

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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-03-2005
"Kevin Ruland" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Shezan Baig
Guest
Posts: n/a
 
      02-03-2005

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-

 
Reply With Quote
 
Kevin Ruland
Guest
Posts: n/a
 
      02-03-2005
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-
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments