gpt4 book ai didi

c++ - C++ 模板特化中声明的范围

转载 作者:搜寻专家 更新时间:2023-10-31 01:24:37 25 4
gpt4 key购买 nike

对于下面的代码:

namespace A
{
struct B
{
using type = std::tuple<struct C>;
};
}

int main()
{
C* ptr = nullptr;
B::C* ptr2 = nullptr;
A::B::C* ptr3 = nullptr;
A::C* ptr4 = nullptr;
}

我只想知道C的范围是什么。我试过 gcc 6.5/7.4/8.3/9.1 和 clang 6/7/8,他们都告诉我 A::C 是正确的。但是我不确定C++标准中是否有任何资料描述了上述情况下C的范围。

你能告诉我你是否知道 C++ 标准中与这个主题相关的资料吗?非常感谢!

最佳答案

这在 C++ 标准的以下部分中有详细说明:

[basic.lookup.elab]

2 If the elaborated-type-specifier is introduced by the class-key and this lookup does not find a previously declared type-name, or if the elaborated-type-specifier appears in a declaration with the form:

   class-key attribute-specifier-seqopt identifier ;

the elaborated-type-specifier is a declaration that introduces the class-name as described in [basic.scope.pdecl].

[basic.scope.pdecl] (emphasis mine)

7 The point of declaration of a class first declared in an elaborated-type-specifier is as follows:

  • [...]
  • ... if the elaborated-type-specifier is used in the decl-specifier-seq or parameter-declaration-clause of a function defined in namespace scope, the identifier is declared as a class-name in the namespace that contains the declaration; otherwise, except as a friend declaration, the identifier is declared in the smallest namespace or block scope that contains the declaration.

模板的参数列表属于“otherwise”子句。 A 是包含声明的最小命名空间,因此类类型 C 是在其中声明的。 A::C 是引用它的正确方法。

关于c++ - C++ 模板特化中声明的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57990045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com