【问题标题】:Name resolution for function declared in anonymous struct匿名结构中声明的函数的名称解析
【发布时间】:2020-11-09 09:11:35
【问题描述】:

ABCf 定义的参数列表中的以下代码中的名称解析是否应该工作?

namespace ns
{
struct A {};
struct S
{
    struct B {};
    struct
    {
        struct C {};
        void f(A, B, C);
    } x;
};
}

#include <type_traits>

void std::type_identity_t<decltype(ns::S::x)>::f(A, B, C) {}

int main()
{
}

其实是works最新clang

【问题讨论】:

标签: c++ namespaces c++17 language-lawyer c++20


【解决方案1】:

[basic.lookup.unqual]/8 对于X 类的成员,在X 定义之外的类成员定义中使用的名称... ,在成员的 declarator-id(24) 之后,应以下列方式之一声明:
...
(8.2) — 应该是 X ... 类的成员,或者
(8.3) — 如果X 是类Y (11.4.10) 的嵌套类,则应是Y 的成员,...或
...
(8.5) — 如果X 是命名空间N 的成员,或者是作为N 成员的类的嵌套类,...在使用名称之前,在命名空间N 中或在N 的封闭命名空间之一中。

脚注 24) 即,出现在例如 parameter-declaration-clausenoexcept-specifier 中的类型中的非限定名称。

这里Xdecltype(ns::S::x)YSNns。因此,A 是通过 (8.5)、B 通过 (8.3)、C 通过 (8.2) 找到的。

【讨论】:

  • 但是std::type_identity_t&lt;decltype(ns::S::x)&gt; 不是一个类(X)。它是别名模板的特化。
  • 这是一个命名类的using 别名。 X 的引用方式无关紧要 - 无论是通过其原始名称、typedef、using 别名、decltype 还是其他方式。
猜你喜欢
  • 2013-05-19
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
相关资源
最近更新 更多