Overload resolution behaviour difference between GCC and clang (SFINAE)

后端 未结 2 1209
情书的邮戳
情书的邮戳 2020-12-15 18:45

GCC accepts the following code:

template 
struct meta
{
    typedef typename T::type type;
};

struct S {};

template 
ty         


        
2条回答
  •  离开以前
    2020-12-15 19:44

    Both Clang and g++ are correct here.

    Per Luc Danton's answer, a compiler is permitted to deduce T = int for the foo function template. Then, during the substitution of that value into the declaration of foo, the implicit instantiation of meta is required, and it results in an error outside the immediate context of the substitution (so SFINAE does not apply). So Clang is correct to reject this code.

    However, [temp.inst]p7 says:

    If the overload resolution process can determine the correct function to call without instantiating a class template definition, it is unspecified whether that instantiation actually takes place.

    Because the non-template foo is an exact match for the arguments in the call, a compiler could determine that a function template specialization will never be the best viable function, and so need not perform argument deduction and substitution. Therefore g++ is correct to not reject this code.

提交回复
热议问题