c++ - §3.3.7/1 第 5 项中突出显示的句子是什么意思?

标签 c++ language-lawyer c++14

§3.3.7/1 第 5 项:

The potential scope of a declaration that extends to or past the end of a class definition also extends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions, nested class definitions, and member function definitions, including the member function body and any portion of the declarator part of such definitions which follows the declarator-id, including a parameter-declaration-clause and any default arguments (8.3.6)).

是否可以在本段给出的第一个示例中识别出这样的声明?

typedef int c;
enum { i = 1 };
class X {
    char v[i];
    int f() { return sizeof(c); }
    char c;
    enum { i = 2 };
};

最佳答案

它看起来好像在说,除此之外,除了上面的答案之外,给定类定义之外的所有代码,即使 X::f在类之外定义,如下所示:

typedef int c;
enum { i = 1 };
class X {
    char v[i];
    int f();
    char c;
    enum { i = 2 };
};

int X::f() {
    return sizeof(c);
}

即,在 X::f 定义的上下文中, c会引用成员变量X::c , 而不是 typedef上面,因为即使它看起来像是在全局定义的,f实际住在X的范围。

关于c++ - §3.3.7/1 第 5 项中突出显示的句子是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31484414/

相关文章:

c++ - 无法在 C++ 中连接字符串?

c++ - MFC:GetWindowRect 用法

c++ - `new auto` 有什么作用?

c# - F#的静态绑定(bind)约束是如何实现的?

c++11 - nullptr 拼写的基本原理(w.r.t. 在例如 unique_ptr 中使用时不使用下划线)

c++ - c++中列表的接口(interface)

c++ - 通用 lambda 的熟悉模板语法

c++ - 幻影类型是否与原始类型具有相同的对齐方式?

pointers - 将 unique_ptr/shared_ptr 与 API 函数结合使用,通过指针将资源作为输出参数返回

c++ - 如何用空指针初始化 unique_ptr 的 vector ?