c++ - 使用声明的类或枚举

标签 c++ class enumeration using language-lawyer

N3797的3.4.3.1/1开头说:

If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-name-specifier is looked up in the scope of the class (10.2), except for the cases listed below.

其中一条规则是:

the lookup for a name specified in a using-declaration (7.3.3) also finds class or enumeration names hidden within the same scope (3.3.10).

你能举个例子来证明这个规则吗?

最佳答案

我相信这就是标准提供的内容:

struct A {
  struct s {} s;
  enum e { e };
};
struct B: A {
  using A::s;
  using A::e;
};
struct B::s s2;
enum B::e e2;

B 范围内的 using-declaration 将类和枚举名称 A::s 纳入范围A::e,即使它们分别被成员和枚举数隐藏。

请注意,using-declaration 还将成员和枚举数纳入范围,因此类和枚举仍隐藏在 B 的范围内;这意味着要在 B 或其他地方使用它们,我们需要使用 structenum 标签。

关于c++ - 使用声明的类或枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24124493/

相关文章:

c++ - 在编译时查找基类

c++ - 编写 DBMS 的技巧

c++ - 错误 : Pointer to incomplete class type is not allowed. 我该怎么做?

arrays - 为什么在 Optional Array 上的映射不同于 non-optionalArray

c++ - 使用 SFML 的 CLion 项目退出代码 -1073741515

c++ - 错误 C2143 : syntax error : missing ',' before '<'

c++ - 发送到 header 中定义的类字符数组

c++ - 全局声明类对象但不带参数

c# - 枚举表单上的控件

示例的 Swift 问题