c++ - Template function argument deduction with an implicit conversion -
i understand template function argument deduction not take implicit conversions account.
so code doesn't compile:
#include <iostream> template<class t> struct {}; struct b : public a<int> {}; struct c { operator b() { return {}; } }; template<class x> void test(a<x> arg1, a<x> arg2) { std::cout << "ok1"; } int main() { b b; c c; test(b, c); // error: no matching function call 'test' } what don't understand how adding level of indirection identity typedef somehow makes work:
#include <iostream> template<class t> struct {}; struct b : public a<int> {}; struct c { operator b() { return {}; } }; template<typename u> struct magic { typedef u type; }; template<class t> using magic_t = typename magic<t>::type; template<class x> void test(a<x> arg1, a<x> arg2) { std::cout << "ok1"; } template<class x> void test(a<x> arg3, magic_t<a<x>> arg4) { std::cout << "ok2"; } int main() { b b; c c; test(b, c); // prints "ok2" } how magic_t<a<x>> end matching c?
the second parameter becomes non-deduced context , doesn't participate in template argument deduction. x deduced first argument.
Comments
Post a Comment