Search code examples
c++language-lawyersfinaetemplate-argument-deductiontype-deduction

What is the exact quote from the C++ Standard that states that type deduction failure on template argument deduction is not an error (SFINAE)?


The following quote from CPPreference is clear:

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.

What are the corresponding quotes from the C++17 Standard (or more recent versions) that state that type deduction failure is not an error?

To the best of my knowledge, paragraph 8 from C++17:17.8.2 ([temp.deduct]) is considered to be the basic quote for SFINAE (Does SFINAE depend on type deduction?, What exactly is the "immediate context" mentioned in the C++11 Standard for which SFINAE applies?):

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, with a diagnostic required, if written using the substituted arguments. [ Note: If no diagnostic is required, the program is still ill-formed. Access checking is done as part of the substitution process. — end note ] Only invalid types and expressions in the immediate context of the function type and its template parameter types can result in a deduction failure. [ Note: The substitution into types and expressions can result in effects such as the instantiation of class template specializations and/or function template specializations, the generation of implicitly-defined functions, etc. Such effects are not in the “immediate context” and can result in the program being ill-formed. — end note ]

One could interpret that, since an invalid type or expression is one that would be ill-formed [...] if..., then it means it is actually not ill-formed in this context. And since type deduction fails with an invalid type or expression, then a type deduction failure is not ill-formed. Another reasoning could be that, as it is not explicitly stated as an error, type deduction is not an error.

However, first, the note states If no diagnostic is required, the program is still ill-formed ("is" in present tense). Besides, although "type deduction fails" is repeated during the section several times, it is not stated what happens when such type deduction fails. I also searched in the sections about overload resolution and the generation of the candidate set (since not having other viable candidates triggers a compilation error), but I could not find an explicit sentence like the CPPreference one: the specialization is discarded from the overload set instead of causing a compile error.

Is there any other more explicit quote I am missing from the Standard stating that when type deduction fails on template argument deduction the program is not ill-formed?

Note: I am not questioning the behavior or the mechanism, I am just looking for more explicit quotes.


Solution

  • The quote mentioned is indeed the first half of SFINAE:

    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, with a diagnostic required, if written in the same context using the substituted arguments.

    [temp.deduct.general]

    Translation: substitution failure results in type deduction failure.

    The second half is:

    If the argument deduction fails or the synthesized function template specialization would be ill-formed, no such function is added to the set of candidate functions for that template.

    [temp.over]

    Translation: type deduction failure results in no corresponding candidate in the overload set.

    The note (notes are not normative) is worded a bit confusingly:

    [Note 5: If no diagnostic is required, the program is still ill-formed. Access checking is done as part of the substitution process. — end note]

    This comes from CWG 1462. The point of this change was to make clear that type deduction failure (and hence SFINAE) only considers diagnosable ill-formedness.