Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is an 'identifier' a 'name' in C++?

When is an 'identifier' called a 'name' in C++? I mostly read that the term `name' is used overly instead of 'identifier' as in the example:

struct S { int i };
S thing1;

In this case, is the thing1 a name or identifer? Or are the terms 'identifier' and 'name' are analogous? In C, is there a use of the term 'name' when referring to an object?

like image 336
cpx Avatar asked Oct 30 '11 21:10

cpx


1 Answers

In C++, the term identifier is just a sequence of digits, letters and _, not starting with a digit. Such an identifier can appear anywhere, and doesn't have to identify anything, despite its name (no pun intended).

The term name associates a meaning with a certain grammar construct. The C++ spec says that one of the following grammar constructs is a name if it denotes an entity (an object, a class, a template and so on) or a label (where you can jump to with goto)

  • identifier
  • template-id (identifier <...>, operator-function-id <...> and literal-operator-id <...>, as in foo <int>).
  • conversion-function-id (operator type, as in operator int)
  • operator-function-id (operator @, as in operator +)
  • literal-operator-id (operator "" identifier, as in operator "" _foo)

For each of these constructs, the rule when a name is the same as another name is defined differently: For identifiers, two names are the same of the sequence is the same (this is only a lexical comparison). For names of form conversion-function-id they are the same if the type used is the same type (so this is a semantic comparison).

As you can see in the example of literal-operator-id, the non-terminal identifier can appear in the grammar in places where it is not a name. So not every identifier is a name, and not every name is an identifier. In the example of template-id, we have a nested use of names. The constructs before the <...> respectively are names again, as they denote declared templates.

like image 121
Johannes Schaub - litb Avatar answered Sep 18 '22 15:09

Johannes Schaub - litb