If the address of a function can not be resolved during deduction, is it SFINAE or a compiler error?

后端 未结 1 1720
南旧
南旧 2020-12-08 03:01

In C++0x SFINAE rules have been simplified such that any invalid expression or type that occurs in the \"immediate context\" of deduction does not result in a compiler error

相关标签:
1条回答
  • 2020-12-08 03:19
    template<class T> void f(T, 
        typename size_map<sizeof(&U::foo)>::type* = 0); 
    

    This doesn't work, because U does not participate in deduction. While U is a dependent type, during deduction for f it's treated like a fixed type spelled with a nondependent name. You need to add it to the parameter list of f

    /* fortunately, default arguments are allowed for 
     * function templates by C++0x */
    template<class T, class U1 = U> void f(T, 
        typename size_map<sizeof(&U1::foo)>::type* = 0); 
    

    So in your case because U::foo does not depend on parameters of f itself, you receive an error while implicitly instantiating S<X> (try to comment out the call, and it should still fail). The FCD says at 14.7.1/1

    The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions or default arguments, of the class member functions, member classes, static data members and member templates;

    That is, if you implicitly instantiate S<X> the following function template declaration will be instantiated

    template<class T> void S<X>::f(T, 
      typename size_map<sizeof(&X::foo)>::type* = 0); 
    

    Analysis on that template declaration will then find that it can't resolve the reference to X::foo and error out. If you add U1, the template declaration will not yet try to resolve the reference to U1::foo (since U1 is a parameter of f), and will thus remain valid and SFINAE when f is tried to be called.

    0 讨论(0)
提交回复
热议问题