I do not understand why T cannot be deduced in this scenario:
template<class T> class MyType { T * data; }; class MyOtherType { }; template<typename T> struct MyType_OutArg { typedef MyType<T> & type; }; template<typename T> void DoSomething(typename MyType_OutArg<T>::type obj) { } void func(MyType_OutArg<MyOtherType>::type obj) { DoSomething(obj); }
From GCC 4.7.1 with -std = C ++ 14
<source>: In function 'void func(MyType_OutArg<MyOtherType>::type)': 26 : <source>:26:20: error: no matching function for call to 'DoSomething(MyType<MyOtherType>&)' DoSomething(obj); ^ 26 : <source>:26:20: note: candidate is: 19 : <source>:19:1: note: template<class T> void DoSomething(typename MyType_OutArg<T>::type) DoSomething(typename MyType_OutArg<T>::type obj) ^ 19 : <source>:19:1: note: template argument deduction/substitution failed: 26 : <source>:26:20: note: couldn't deduce template parameter 'T' DoSomething(obj); ^ Compiler returned: 1
Of course, the following works:
DoSomething<MyOtherType>(obj);
but I'm not sure why this is necessary. Should the compiler have enough information?
c ++ templates
user109078
source share