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

标签 c++ language-lawyer

代码示例:

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/

相关文章:

c++ - std::forward 与 std::move 的用法

c++ - 是使用引入一个新的成员函数还是只是一个别名?

c - `free(a_comparable_pointer)` 是定义明确还是 UB?

c++ - 我可以依赖无序 map 的顺序吗?

c++ - memcpy 是可简单复制的类型构造还是赋值?

c++ - 在opencv中创建透明图像

c++ - 如何使用 GLM vector 关系函数?

c++ - 只是 C++ 中的事件

c++ - gdb : examining whatis to intelligently print values

c++ - 为什么在 std::aligned_storage 中定义扩展对齐实现