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

type convertion between template

Hi, I've got a problem while trying to make my source code clean.

In a template:

template<typename T>
class A
{
public:
T data;
};

may I write a type convertion operator that can convert A<T1> to A<T2>
?
I guess it will be something like this, but failed I am.

template<typename T>
class A
{
public:
T data;
template<typename T2> operator A<T2> () const
{
A<T2> temp;
temp.data = (T2)data;
return temp;
}
};

Sincerely,

kaikai

Jan 3 '06 #1
4 2227
kaikai wrote:
may I write a type convertion operator that can convert A<T1> to A<T2>
?
Yes. An instantiated template is a type, and you can use it wherever
you would use a type, including in a conversion operator.
I guess it will be something like this, but failed I am.

template<typename T>
class A
{
public:
T data;
template<typename T2> operator A<T2> () const
{
A<T2> temp;
temp.data = (T2)data;
return temp;
}
};


Works fine for me. What's the problem on your end?

By the way, don't use those old C-style casts! They're very unsafe.
Use static_cast.

Luke

Jan 3 '06 #2

Luke Meyers wrote:
kaikai wrote:
may I write a type convertion operator that can convert A<T1> to A<T2>
?
Yes. An instantiated template is a type, and you can use it wherever
you would use a type, including in a conversion operator.
I guess it will be something like this, but failed I am.

template<typename T>
class A
{
public:
T data;
template<typename T2> operator A<T2> () const
{
A<T2> temp;
temp.data = (T2)data;
return temp;
}
};


Works fine for me. What's the problem on your end?


I got error c2440 under msvc++6 sp6,

error C2440: 'type cast' : cannot convert from 'struct _Point2<int>' to
'struct _Point2<float>'
No constructor could take the source type, or constructor
overload resolution was ambiguous

cods:
template< typename T >
struct _Point2
{
T x, y;
template< typename T2 >
operator _Point2<T2> () const
{
_Point2<T2> t;
t.x = T2(x);
t.y = T2(y);
return t;
}
};

// in a function...
Point2f a;
Point2i b;
a = Point2f(b); // here got the error

By the way, don't use those old C-style casts! They're very unsafe.
Use static_cast.

Luke


kaikai

Jan 3 '06 #3
kaikai wrote:
I got error c2440 under msvc++6 sp6,


Well, there's your problem right there. MSVC 6 has crappy support for
real template programming. Sorry, but you're stuck. Download a better
compiler, there are many excellent ones freely available. And the MSVC
6 IDE is pretty dated by now, too...

Luke

Jan 3 '06 #4
Luke Meyers wrote:
kaikai wrote:
I got error c2440 under msvc++6 sp6,


Well, there's your problem right there. MSVC 6 has crappy support for
real template programming. Sorry, but you're stuck. Download a better
compiler, there are many excellent ones freely available. And the MSVC
6 IDE is pretty dated by now, too...

Luke


OIC, thank you :-)

kaikai

Jan 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Brown | last post by:
Hello all, Is there a way to determine a variable's type at run-time? The reason I'm asking is that i have code that looks like this: template <class T> Object::Object(int TypeCode, T* data)...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
2
by: Javier | last post by:
Hi All I'm in a nightmare trying to do a simple c++ program... I want to read a file that will have strings in a vector. The path where is the file is in a ini file. I did:
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
5
by: Fabio Fracassi | last post by:
Hi, I belive what i am trying to do is not possible, but I hope someone can change my mind. Here is some code i'd like to write: template <class type> class engine1 {}; template <class...
5
by: Axter | last post by:
I'm fine tuning a scope_handle class that takes a policy class as the second template. http://code.axter.com/scope_handle.h Please see above link for full understanding of the problem. One...
11
by: Demorsy | last post by:
I want to convert a string (or object) to a primitive type. But I don't know the type to convert to at run time. For example l have variable (lets say its an int): int unknownType = 0; And a...
4
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
7
by: Ben R. | last post by:
How does automatic type casting happen in vb.net? I notice that databinder.eval "uses reflectoin" to find out the type it's dealing with. Does vb.net do the same thing behind the scenes when an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.