c++ - 相关名称的参数相关查找

标签 c++ language-lawyer linkage argument-dependent-lookup dependent-name

This description on cppreference.com

The lookup of a dependent name used in a template is postponed until the template arguments are known, at which time [...] ADL examines function declarations with external linkage that are visible from either the template definition context or the template instantiation context.

与此相反的是下面的代码片段compiles fine使用三个编译器(MSVC、clang、gcc):

template <class T>
void CallFoo ()
{
    Foo (T ());
}


class Apple {};


int main ()
{
    CallFoo<Apple> ();
}


static void Foo (Apple)
{
}

FooCallFoo 中的依赖名称:它依赖于模板参数 T。但是函数 Foo 被编译器找到,尽管它违反了上面引用的两个规则。

  • Foo 的声明在 CallFoo 的定义或实例化中都不可见,因为它在两者之下。
  • Foo 有内部链接。

不太可能所有三个编译器都有错误。我可能误会了什么。你能详细说明一下吗?

最佳答案

在 C++03 中,匿名命名空间的成员可以有外部链接,尽管在其他翻译单元中是不可命名的。因此,从依赖 ADL 中排除实际的 static 函数被认为是允许的。在 C++11 中,匿名命名空间强加了内部链接,因此限制变得不合理。然而,尽管实现采用了新行为并在 2011 年立即提交了一个问题(如评论中所述),但措辞仍保留在两个地方,直到 N4810。 2019 年 3 月。

至于函数的放置,这是具有 multiple points of instantiation 的函数的产物,包括实例化它们的任何翻译单元的结尾(对 C++20 中的模块进行了轻微调整);如果实例化函数模板对不同的选择产生不同的结果,则程序格式错误,不需要诊断(如评论中所述)。

关于c++ - 相关名称的参数相关查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57663135/

相关文章:

c++ - 将内部类名称声明为另一个声明的一部分

c++ - 插入集合时出现段错误

c++ - NoFieldsClass::operator new(std::size_t):标准是否允许它在每次调用时返回相同的地址?

c++ - 列出聚合的初始化 : when can it invoke copy constructor?

c++ - 使用 C 样式链接从函数返回类模板安全吗?

c - 如果存在先前的静态声明,为什么隐式 extern 声明无效?

c++ - matlab mex 文件和 C++ dll (windows)

c++ - 编译器误区

c++ - 你能尽可能多地使用 'this' 使这段代码更理想吗?

c++ - bool 乘法