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?
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 <...>
, operator-function-id <...>
and literal-operator-id <...>
, as in foo <int>
).operator type
, as in operator int
)operator @
, as in operator +
)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With