Did I missed something here?
Code:
template<typename T>class A {
public:
template<typename T2> A<T2> toA() {
return A<T2>();
}
};
In the main function, this code just works fine:
Code:
A<int> a1;
A<float> a2 = a1.toA<float>(); // Works fine! As expected.
I think there is nothing wrong with the code,
but G++ gives me syntax errors:
Code:
template<typename T>class B {
A<T> a;
public:
B( void ) { ; }
B( const A<T> &a ) { this->a = a; }
template<typename T3> B<T3> toB() {
return B<T3>( a.toA<T3>() ); // error?
// ???.cpp: In member function `B<T3> B<T>::toB()':
// ???.cpp:9999: error: expected primary-expression before '(' token
// ???.cpp:9999: error: expected primary-expression before '>' token
// ???.cpp:9999: error: expected primary-expression before ')' token
}
};
I tried to compile it in VC and it gave me a strange error message:
Code:
B<int> b;
b.toB<double>(); // x:\somewhere\code.cpp(9999) : error C2062: type 'double' unexpected
And then I tried another way:
Code:
template<typename T3> B<T3> toB() {
A<T3> a1 = this->a.toA<T3>(); // won't work
A<T> a2 = this->a;
A<T3> a3 = a2.toA<T3>(); // silly, this way won't work too
}
Thank's in advance.
EDIT: Sorry for my English.. T.T