Question

#include <iostream>
#include <functional>
template<typename T>
struct id { typedef T type; };

template<typename T>
void f(T b, typename id<T>::type* a){}

int main() {
   f(0, 0);
}

vs2013: OK!

g++4.8.2:compile error,such is the info:

main.cpp: In function 'int main()':
main.cpp:10:10: error: no matching function for call to 'f(int, int)'
     f(0,0);
          ^
main.cpp:10:10: note: candidate is:
main.cpp:7:6: note: template<class T> void f(T, typename id<T>::type*)
 void f(T b, typename id<T>::type* a){}
      ^
main.cpp:7:6: note:   template argument deduction/substitution failed:
main.cpp:10:10: note:   mismatched types 'typename id<T>::type*' and 'int'
     f(0,0);
          ^
Was it helpful?

Solution

The reason is that the Standard has been unclear what happens with non-deduced contexts that have part of compound types (pointer stars for example), thereby mismatching an argument but that still can accept the argument by implicit conversions

Issue http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1184 did fix this by adding a note that says that similar to the situation where a function parameter contains no template parameters that are deduced, implicit conversions should be allowed aswell to bridge a mismatch.

Since then, other issues have been found regarding the treatment of these "implicit conversions" during argument deduction for template parameters, and handled by http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1391 .

Overall, I think the effect of 1184 is that GCC should accept your code, but because of the issues in other cases reflected in #1391, they may have delayed their implementation of #1184 until the exact details are worked out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top