-
March 20th, 2009, 07:07 PM
#1
[RESOLVED] Template Cast Operator. Possible?
I'm trying to do something in my code and it's giving me an error on compile. What I'm trying to do is put a template cast operator in a container class. Here's the simplified relevant code:
Code:
template<class T> class Container
{
public:
template<class U> Container<U> operator U (void)
{
// Convert each T element in 'list' to U elements in new Container, then return the container
}
protected:
std::vector<T> list; // Contained objects
};
The operator method is actually defined in a separate file, if that matters.
Following is the GCC error given:
error: operator 'U' declared to return 'Container<U>'
I don't even know if this is possible. I need to know whether or not it is possible, and if so then how. What am I doing wrong? Otherwise, if anyone could come up with an alternative way to do this, I would greatly appreciate it. And if you're wondering, yes I have a good reason for wanting to do this. Thanks.
Last edited by Etherous; March 20th, 2009 at 07:10 PM.
-
March 20th, 2009, 07:42 PM
#2
Re: Template Cast Operator. Possible?
Type conversion functions like that one don't allow a return type to be specified. I'm not sure if it is possible myself, but I know that you can't specify a return type and that's most likely the reason for your error.
-
March 20th, 2009, 08:07 PM
#3
Re: Template Cast Operator. Possible?
Oh, right! Yeah, I'm an idiot. It will build now, but I won't have a chance to test it for a while. Thanks
Edit:
Actually it will still mess up, as it will try to return a 'U' instead of a 'Container<U>'. I've fixed it and it should work now. Here is the modified code (just the operator):
Code:
template<class U> operator Container<U> (void)
{
// Convert each T element in 'list' to U elements in new Container, then return the container
}
Again, I won't be able to test it, but it seems to work
Last edited by Etherous; March 20th, 2009 at 10:07 PM.
-
March 20th, 2009, 10:12 PM
#4
Re: Template Cast Operator. Possible?
Here is an example of a type conversion function:
Code:
template <typename Lo, typename Hi> Lo down_cast (const Hi& value)
{
Lo result = Lo (a);
if (Hi (result) != value)
throw exception ("loss of data in down cast");
return result;
}
So in your case you might want to do something like this (untested) code:
Code:
template <typename Tout, typename Tin>
vector <Tout> ConvertStuff (const vector <Tin>& vIn)
{
vector <Tout> vOut;
for (int i = 0; i < vIn.size (); ++i)
{
Tout value = vIn [i];
vOut.push_back (value);
}
return vOut;
}
As long as you have an assignment operator to convert the Tin to a Tout you should be okay.
-
March 23rd, 2009, 06:16 AM
#5
Re: [RESOLVED] Template Cast Operator. Possible?
You can use the copy constructor if type OutT has an assignment operator that accepts type InT instead of manual loops. Furthermore, it allows RVO.
Code:
template<typename InT, typename OutT>
vector<OutT> convert( const vector<InT>& op )
{
return vector<OutT>( op.begin(), op.end() );
}
- Guido
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|