来自 cppreference 的定义:
Non-static data members are the variables that are declared in a member specification of a class.
他们有例子:
class S { int& r; // non-static data member of reference type };
但我们知道非静态数据成员引用不是变量,因为标准:
§3/6: A variable is introduced by the declaration of a reference other than a non-static data member or of an object.
那么他们对非静态数据成员的定义是否错误(他们忘记了这个异常)?在哪里可以找到术语“非静态数据成员”的正确定义?
很遗憾,我在 C++ 标准中找不到非静态数据成员的定义。
编辑:来自cppreference object definition和下面的讨论我们可以得出结论,非静态数据成员根本不是对象。和 cppreference non-static member page更正了目前讨论的定义。
最佳答案
So their definition of non-static data member is wrong
是的,在数据成员页面的介绍性句子中使用“变量”这个词是错误的(而且,正如评论中提到的,它是一个 wiki,wiki 页面上的讨论选项卡获得更快的反馈)。
当前的标准写法是 3[basic]/6
和:
A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable’s name, if any, denotes the reference or object.
因此,引用数据成员被显式排除,要弄清楚其余部分,您需要 1.8[intro.object]/1
An object is created by a definition (3.1), by a new-expression (5.3.4), when implicitly changing the active member of a union (9.3), or when a temporary object is created (4.4, 12.2).
最后 3.1[basic.def]/2
A declaration is a definition unless ... it declares a non-inline static data member in a class definition (9.2, 9.2.3),
虽然看起来变量和数据成员之间的区别是不切实际的语言律师主义,但在理解编译器诊断时它实际上很重要,至少在这种情况下:
struct X {
int m;
void f() { auto l = [m](){ return m; }; }
};
海湾合作委员会:
error: capture of non-variable 'X::m'
clang :
error: 'm' in capture list does not name a variable
国际刑事法院:
error: member "X::m" is not a variable
关于c++ - 非静态数据成员的cppreference定义是否错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41148054/