Y
ymost
Hi all,
I've recently learned that conversion operators don't work with
template classes. I have tried looking for an explanation for this,
but did not find a satisfactory one. Consider the following situation:
// -------------------------------------------------------------------
template<typename T> struct A {};
template<typename T> struct B {
operator A<T>() {return A<T>();}
};
template<typename T> void func(A<T> a) {}
int main() {
B<int> b;
func(b); // <---- cannot deduce template argument
return 0;
}
// -------------------------------------------------------------------
Both MS VC and gcc fail to compile the marked line.
As far as I can see, there are no circumstances in which allowing this
implicit conversion will cause ambiguity, so why isn't it allowed?
Also: is there a workaround?
I've recently learned that conversion operators don't work with
template classes. I have tried looking for an explanation for this,
but did not find a satisfactory one. Consider the following situation:
// -------------------------------------------------------------------
template<typename T> struct A {};
template<typename T> struct B {
operator A<T>() {return A<T>();}
};
template<typename T> void func(A<T> a) {}
int main() {
B<int> b;
func(b); // <---- cannot deduce template argument
return 0;
}
// -------------------------------------------------------------------
Both MS VC and gcc fail to compile the marked line.
As far as I can see, there are no circumstances in which allowing this
implicit conversion will cause ambiguity, so why isn't it allowed?
Also: is there a workaround?