我想了解为什么调用模板f
下面的代码无法编译:
struct A
{
template<class...>
friend void f(A) { }
} x;
int main()
{
f<>(x);
}
ADL 要求函数调用中的后缀表达式是非限定 ID。 simple-template-id ( f<>
) 不是一个 unqualified-id 吗?
最佳答案
相关条款似乎是7.3.1.2 [namespace.memdef]第3段:
Every name first declared in a namespace is a member of that namespace. If a friend declaration in a non-local class first declares a class, function, class template or function template the friend is a member of the innermost enclosing namespace. The friend declaration does not by itself make the name visible to unqualified lookup (3.4.1) or qualified lookup (3.4.3). ...
也就是说,找到该名称的唯一方法是通过 ADL。但是,要应用模板参数,需要根据 14.2 [temp.names] 第 2 段找到名称:
For a template-name to be explicitly qualified by the template arguments, the name must be known to refer to a template.
关于c++ - ADL 未选取带有模板参数列表的后缀表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28006660/