Is this program ill-formed despite SFINAE?

后端 未结 2 1043
情话喂你
情话喂你 2021-01-21 13:08
template  void f() {
    return 0;  // returning value from function returning `void`
}

int main()
{
    // Not instantiating or calling any f

        
相关标签:
2条回答
  • 2021-01-21 13:28

    Reading more closely, that standard passage says:

    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. [..]

    return 0 is not an expression, so SFINAE does not apply.

    The passage goes on:

    Only invalid types and expressions in the immediate context of the function type and its template parameter types can result in a deduction failure.

    return 0 has nothing to do with the function type or its template parameter types, so SFINAE still does not apply.

    0 讨论(0)
  • 2021-01-21 13:31

    The program is ill-formed as per 14.6/8:

    If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required.

    That is whether you instantiate the template or not, the template definition is ill-formed as there is no possible instantiation that will succeed.

    Note that this is completely unrelated to SFINAE: Substitution Failure is not an Error is part of the substitution process, and never takes into account the contents of the template.

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