c++ - ADL 未选取带有模板参数列表的后缀表达式

标签 c++ c++11 argument-dependent-lookup

我想了解为什么调用模板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/

相关文章:

c++ - SDL - 动态阿尔法?

java - 单行函数是否需要大括号?

c++ - 是否有 "safe"static_cast 替代方案?

c++ - 缺少用户定义的to_string()的编译时检测

c++ - Koenig Lookup 的奇怪行为

c++ - 初始化 SDL_Mixer 给出错误 "No available audio device"

c++ - QQuickWidget 将信号从 C++ 发送到 QML 中的插槽

c++ - 没有用于调用可变参数包函数的匹配函数

c++ - 智能指针控制 block 内部机械

c++ - ADL 名称查找问题,正在使用 std::swap; swap(a,b) 与函数重载或内部作用域函数隐藏外部作用域函数有关?