Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and where is it possible that a variable does not has an associated name in C++?

In the C++17 standard is stated (emphasize mine):

"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."

Source: ISO/IEC 14882:2017 (C++17), §6/6 - "Basic concepts"

Why the "if any"? Can a variable omit a name in C++?

If I look to cppreference:

"In C++, a variable is actually just a bit of memory that has been reserved for the program’s use. You refer to it using a variable name, so you don’t need to worry about where it is in memory (though you can find out its memory address, and even specify its location, if you want)."

Source: https://en.cppreference.com/book/intro/variables

Or Wikipedia (I know it's not the best source but still common):

"In computer programming, a variable or scalar is a storage address (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context."

Source: https://en.wikipedia.org/wiki/Variable_(computer_science)

This says a variable shall always provide a name associated with it, regardless of whether the referenced object/value is accessed by it or not.


  • Is it possible that a variable not has a name in C++?

  • And if yes, how and where (if multiple cases are possible)?

Or if I've misunderstood something, How is the "if any" then to be interpreted?


Related:

What is the difference between a variable, object, and reference?

like image 985
RobertS supports Monica Cellio Avatar asked Jun 23 '20 14:06

RobertS supports Monica Cellio


People also ask

Where are variables names stored in C?

Variable names don't exist anymore after the compiler runs (barring special cases like exported globals in shared libraries or debug symbols). The entire act of compilation is intended to take those symbolic names and algorithms represented by your source code and turn them into native machine instructions.

Can variable name start with in C?

A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore.


Video Answer


1 Answers

As mentioned in the comments,

void foo(int) {}
//          ^

defines a variable without a name.

[basic.pre]/6:

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

It's not a reference obviously, but is it an object? Yes.

[intro.object]/1

An object is created by a definition, [...]

and a parameter of a function definition is a definition ([basic.def]/2).

like image 128
Rakete1111 Avatar answered Oct 23 '22 03:10

Rakete1111