I would like to be able to cast a class A<double> to A<float>. Here is what I tried:
#include<iostream>
template<class T1>
class A {
template<class T0>
operator A() {
std::cout << __PRETTY_FUNCTION__ << "\n";
}
};
template<class T0,class T1>
void bar( const A<T0>& a, const A<T1>& b )
{
reinterpret_cast< const A<T0> >(b);
}
int main()
{
A<float> a_f;
A<double> a_d;
bar(a_f,a_d);
}
The compiler spits: error: invalid cast from type ‘const A<double>’ to type ‘const A<float>’.
Is a conversion operator converting to a class differing only in the template argument possible? If yes: How?
Without the template argument, its a conversion to
A<T1>, i.e. a no-op.