473,239 Members | 1,832 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,239 software developers and data experts.

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 1365
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: bartek | last post by:
Please consider the following scenario below (sketch of). There are two templates defined: A and B, both with mutual conversion operators defined. Also, there's a free function template...
14
by: ES Kim | last post by:
Consider: #include <string> #include <iostream> using namespace std; struct S { const char* ps_; operator string();
9
by: Tanmoy Bhattacharya | last post by:
Hi, This is a question about whether I am right that a particular syntactic sugar is missing in C++. Let me explain with an example. Let us say I have a class for complex numbers, and I want...
3
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
2
by: Clark S. Cox III | last post by:
I'm writing a class that, depending on a template parameter, has constructors that take differing numbers of arguments. I initially thought that I could use SFINAE (via boost::enable_if_c) to...
15
by: Alexander Stippler | last post by:
hi, the following does not work. I do not understand why, since it works if I replace line marked by (1) with the line below ((2)). Compiling with gcc4.0 results in "error: conversion from...
2
by: coder_lol | last post by:
MS VS 7.0 happily resolves by SmartPointer and Inheritance, but I got to use another target compiler and it does not accept user conversion for templates. Can I forced a recast somehow? I have...
6
by: Peng Yu | last post by:
Hi, I want B has all the constructors that A has. Obviously, the code below would not work. I could define a corresponding B's constructor for each A's constructor. But if A has many...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.