This rule applies during overload resolution of function templates: When substituting the explicitly specified or deduced type for the template parameter fails, the specialization is discarded from the overload set instead of causing a compile error.
// ellipsis parameter has the lowest ranking for overload resolution void(...) { std::cout << "Catch-all overload calledn"; } // this overload is added to the set of overloads if // C is a reference-to-class type and F is a pointer to member function of C // (void)(c.*f)(), void() // the return type is void() // but (void)(c.*f)(), void() execute comma template <classC, classF> auto (Cc, Ff) -> decltype((void)(c.*f)(), void()) { std::cout << "Reference overload calledn"; } // this overload is added to the set of overloads if // C is a pointer-to-class type and F is a pointer to member function of C template <classC, classF> auto (Cc, Ff) -> decltype((void)((c->*f)()), void()) { std::cout << "Pointer overload calledn"; } structX {voidf(){} }; intmain(){ X x; test( x, &X::f); test(&x, &X::f); test(42, 1337); } //output // Reference overload called // Pointer overload called // Catch-all overload called
近期评论