gpt4 book ai didi

c++ - 使用 'using' 指令隐藏名字

转载 作者:行者123 更新时间:2023-12-01 14:20:19 25 4
gpt4 key购买 nike

代码示例:

struct A {};
struct B { using A = A; };

int main()
{
B b;
}

Clang 编译它。但是 GCC 给出了以下错误 ( demo ):

declaration of 'using A = struct A' changes meaning of 'A'

C++ standard说:

If a class name ([class.name]) or enumeration name ([dcl.enum]) and a variable, data member, function, or enumerator are declared in the same declarative region (in any order) with the same name (excluding declarations made visible via using-directives ([basic.lookup.unqual])), the class or enumeration name is hidden wherever the variable, data member, function, or enumerator name is visible.

UPD.0:感谢来自莫斯科的 Vlad

A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule

这是否意味着 GCC 行为不正确?谢谢!

最佳答案

看来是gcc的bug。根据 C++ 20 标准(6.3.7 类作用域)

2 A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.

在这种情况下

struct A {};
struct B { using A = A; };

名称 B::A 指的是 struct A 的相同声明。

这是 C++ 标准中的一个示例,它显示了引号的含义。

typedef char* T;
struct Y {
T a; // error: T refers to ::T but when reevaluated is Y::T
typedef long T;
T b;
};

关于c++ - 使用 'using' 指令隐藏名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61455507/

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