8

As the title suggests, this question has been asked before. However, the answers pertained to C++03/0x(11). C++11 (N3337) says this about variables:

[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 denotes the reference or object.

This may imply that variables are essentially named objects/references.

However, in C++14/C++17, that last sentence was changed to

The variable’s name, if any, denotes the reference or object.

which implies that a variable does not necessarily have a name.

A different interpretation of the first sentence may suggest that a variable is a name, since a name denoting an object/reference is also introduced by a declaration of such entities. But the second sentence contradicts that notion with the phrase "variable's name". So, is variable now just a hypernym for object and reference, whether named or not?

TSmith
  • 405
  • 3
  • 10
  • pointer to a variable is not a name but the variable exist. – vcloarec Jun 29 '18 at 18:17
  • @vcloarec a pointer itself must have a name. An r-value produced by taking the address of a variable is not a variable. – George Jun 29 '18 at 18:18
  • 1
    Possibly answered by https://stackoverflow.com/a/31234708, but not really a duplicate question, since that other question didn't ask about this aspect. –  Jun 29 '18 at 18:19
  • @George : I wanted to illustrate that a variable does not necessarily have a name. – vcloarec Jun 29 '18 at 18:22
  • @vcloarec "pointer to a variable is not a name but the variable exist." doesn't illustrate that. I'll admit to not knowing what the "if any" part actually means, but again, a pointer variable must have a name, and an r-value produced by taking the address of an object (pointer or not) is not automatically a variable. – George Jun 29 '18 at 18:25
  • @George : I aggree with you, the address of the object is not automatically a variable and a pointer variable must have a name. But, i think a object (a variable) mustn't have to have a name. So that's why the "if any". An other example is the exception on the answer of Barry. – vcloarec Jun 29 '18 at 18:36

1 Answers1

13

This change was a result of CWG 1769, addressing the status of exception objects bound to unnamed catch handler parameters:

catch (std::exception&) // <==
{ 
}

That is now a variable. This simplifies the conceptual model around exception objects.

The first sentence, which remained unchanged, is still the complete definition of the term variable.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • So, all _declared_ objects and references (except references that are non-static data members), whether named or unnamed, are variables? – TSmith Jun 29 '18 at 18:31