5
votes

Definition from cppreference:

Non-static data members are the variables that are declared in a member specification of a class.

And they have the example:

class S
{
   int& r;               // non-static data member of reference type
};

But we know that non-static data member references are not variables because of the Standard:

§3/6: A variable is introduced by the declaration of a reference other than a non-static data member or of an object.

So is their definition of non-static data member wrong (they forgot about this exception)? Where I can find correct definition of the term "non-static data member"?

Unfortunately I couldn't find a definition of non-static data member in the C++ Standard.

EDIT: From cppreference object definition and discussion below we can conclude that non-static data members are not objects at all. And cppreference non-static member page corrected the discussed definition at the moment.

1
If you declare r as a non-static class member then it's still a non-static member variable. It just happens to be a variable that references something else. If you had that definition as a "normal" function-local variable, would you still say it's not a variable?Some programmer dude
@Someprogrammerdude C++ Standard says that it is not true.Rodvi
Non-reference non-static data member definitions don't introduce variables either, so, yes, the wording on cppreference is incorrect in general, not just for references.bogdan
@KonradRudolph We have to be very careful with terminology here. A non-reference non-static data member definition does not define an object (just like the enclosing class definition doesn't define an object of that class); it defines a class member, period. Think about it this way: member subobjects need an enclosing object, otherwise, what are they subobjects of? Yes, when an object of the enclosing class is defined, then that definition defines its member subobjects as well; different ones for each object. The non-static member definition doesn't define any of those subobjects.bogdan
In other words, there is a "1:n" relationship between a data member and its objects/references.Johannes Schaub - litb

1 Answers

3
votes

So their definition of non-static data member is wrong

Yes, it was wrong to use the word "variable" in the introductory sentence of the data members page (and, as mentioned in the comment, it's a wiki, the discussion tabs on wiki pages get faster feedback).

The current standard wording is 3[basic]/6 and :

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.

So, reference data members are excluded explicitly, and to figure out the rest you need the definition of "object" from 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).

And finally 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),

Although it may seem like the distinction between variables and data members is impractical language-lawyerism, it is actually important when understanding compiler diagnostics, at least in this case:

struct X {
    int m;
    void f() { auto l = [m](){ return m; }; }
};

gcc:

error: capture of non-variable 'X::m' 

clang:

error: 'm' in capture list does not name a variable

icc:

error: member "X::m" is not a variable