c++ 11在类的完整作用域中命名重求值

C++11 name re-evaluation in completed scope of a class?

本文关键字:作用域 c++      更新时间:2023-10-16

在c++ 3.3.7.2中说[basic.scope.class]

在类S中使用的名称N应在其上下文中以及在S的完整范围内重新计算时引用相同的声明。

在类S中使用的名称N在其上下文中引用的声明与在S的完整作用域中重新求值时引用的声明不同的翻译单元的例子是什么?

struct X {};
struct Y {};
typedef X N;
struct S
{
    N n;
    typedef Y N;
};
$ g++ test.cpp 
9:15: error: declaration of ‘typedef struct Y S::N’ [-fpermissive]
4:11: error: changes meaning of ‘N’ from ‘typedef struct X N’ [-fpermissive]