implicit instantiation of undefined template 'class'

后端 未结 1 700
深忆病人
深忆病人 2021-01-02 00:35

When trying to offer functions for const and non-const template arguments in my library I came across a strange problem. The following source code is a minimal example pheno

1条回答
  •  借酒劲吻你
    2021-01-02 00:57

    The reason is the way function call resolution is performed, together with template argument deduction and substitution.

    1. Firstly, name lookup is performed. This gives you two functions with a matching name foo().

    2. Secondly, type deduction is performed: for each of the template functions with a matching name, the compiler tries to deduce the function template arguments which would yield a viable match. The error you get happens in this phase.

    3. Thirdly, overload resolution enters the game. This is only after type deduction has been performed and the signatures of the viable functions for resolving the call have been determined, which makes sense: the compiler can meaningfully resolve your function call only after it has found out the exact signature of all the candidates.

    The fact that you get an error related to the non-const overload is not because the compiler chooses it as a most viable candidate for resolving the call (that would be step 3), but because the compiler produces an error while instantiating its return type to determine its signature, during step 2.

    It is not entirely obvious why this results in an error though, because one might expect that SFINAE applies (Substitution Failure Is Not An Error). To clarify this, we might consider a simpler example:

    template struct X { };
    
    template typename X::type f(T&) { }  // 1
    template void f(T const&) { }           // 2
    
    int main()
    {
        int const i = 0;
        f(i); // Selects overload 2
    }
    

    In this example, SFINAE applies: during step 2, the compiler will deduce T for each of the two overloads above, and try to determine their signatures. In case of overload 1, this results in a substitution failure: X does not define any type (no typedef in X). However, due to SFINAE, the compiler simply discards it and finds that overload 2 is a viable match. Thus, it picks it.

    Let's now change the example slightly in a way that mirrors your example:

    template struct X { };
    
    template
    struct R { typedef typename X::type type; };
    
    // Notice the small change from X into R!
    template typename R::type f(T&) { }  // 1
    template void f(T const&) { }           // 2
    
    int main()
    {
        int const i = 0;
        f(i); // ERROR! Cannot instantiate R
    }
    

    What has changed is that overload 1 no longer returns X::type, but rather R::type. This is in turn the same as X::type because of the typedef declaration in R, so one might expect it to yield the same result. However, in this case you get a compilation error. Why?

    The Standard has the answer (Paragraph 14.8.3/8):

    If a substitution results in an invalid type or expression, type deduction fails. An invalid type or expression is one that would be ill-formed if written using the substituted arguments. [...] Only invalid types and expressions in the immediate context of the function type and its template parameter types can result in a deduction failure.

    Clearly, the second example (as well as yours) generates an error in a nested context, so SFINAE does not apply. I believe this answers your question.

    By the way, it is interesting to notice, that this has changed since C++03, which more generally stated (Paragraph 14.8.2/2):

    [...] If a substitution in a template parameter or in the function type of the function template results in an invalid type, type deduction fails. [...]

    If you are curious about the reasons why things have changed, this paper might give you an idea.

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