7

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?

  • 7
    `void foo(int /*no name*/)`? – user7860670 Jun 23 '20 at 14:51
  • @user7860670 Is this a (real) variable? I can't see that it denotes a reference or object. How is that a variable? Can a comment be treated as variable? – RobertS supports Monica Cellio Jun 23 '20 at 14:53
  • 2
    It is a function parameter, comment is there just as placeholder. – user7860670 Jun 23 '20 at 14:58
  • 3
    @RobertSsupportsMonicaCellio A variable is the declaration of any object or reference as quoted in the question. The function declares an argument, it is an object of type `int`. So yes, it is a variable. A comment is not a declaration, so no, it is not a variable. – François Andrieux Jun 23 '20 at 15:03
  • @user7860670 I've thought this is only valid for functions declarations/prototypes to omit the name, just to illustrate the parameter type. How does that denote an object? – RobertS supports Monica Cellio Jun 23 '20 at 15:04
  • Note that some languages use the term "variable" for what C++ calls "object". The terminology in Java has some overlap with C++, but not entirely. In some languages, the term "variable" exists, but denotes things that are not variable (and are not necessarily stored anywhere).You need to learn the terminology of every language on its own terms. – molbdnilo Jun 23 '20 at 15:06
  • Wouldn't any [temporary object](https://en.cppreference.com/w/cpp/language/lifetime#Temporary_object_lifetime) be a variable without a name? Just `int main() { [](void){}; }` creates a lambda, an object, without a name. – KamilCuk Jun 23 '20 at 15:09
  • 2
    @RobertSsupportsMonicaCellio Function definition can also omit the argument names, if the argument is not used. – François Andrieux Jun 23 '20 at 15:10
  • 2
    @KamilCuk That creates a nameless object, but it is not a variable because it is not a **declaration** of an object or reference. – François Andrieux Jun 23 '20 at 15:11
  • @KamilCuk no, because it wouldn't be a "declaration [...] of an object". – Rakete1111 Jun 23 '20 at 15:11
  • @molbdnilo I know, but it was the most prominent question regarding the observations of the terms on SO. Thus, marked as "Related". If you recommend to remove it, I'll take it out. – RobertS supports Monica Cellio Jun 23 '20 at 15:13
  • 2
    Does this answer your question? [Follow-up: What exactly is a variable in C++14/C++17?](https://stackoverflow.com/questions/51107333/follow-up-what-exactly-is-a-variable-in-c14-c17) – Language Lawyer Jun 23 '20 at 15:16
  • @LanguageLawyer The proposed question could indeed be seen as duplicate (or better: this one could be seen as duplicate of this) , although the made answer there not really answers all of my issues. If you would ask me "Does this answer your question?" I would say no. – RobertS supports Monica Cellio Jun 23 '20 at 15:19
  • Would anonymous union member count, as it has no name and is a member variable? – eerorika Jun 23 '20 at 15:30
  • That question answers _Is it possible that a variable not has a name in C++? And if yes, how?_ and I don't see anything else in your question in addition to this. Comparing the C++ Standard with Wikipedia is invalid. The Standard has the definition of _variable_ and if this word is used somewhere outside the Standard with different meaning, it is just a coincidence and means nothing. – Language Lawyer Jun 23 '20 at 15:33
  • @LanguageLawyer As I said, I myself admit that this question probably is a duplicate, but to answer the question: "Does that answer your question?" I clearly say no, because the one and only answer there is not very helpful for me. Rakete1111 and other users mentioned things which were not explained in this answer. If there had been a more detailed answer, I would answer this question with yes. – RobertS supports Monica Cellio Jun 23 '20 at 15:36
  • @LanguageLawyer Now I added a quote from [cppreference](https://en.cppreference.com/book/intro/variables). This will indeed be a better reference. – RobertS supports Monica Cellio Jun 23 '20 at 15:57
  • Well, if you'd like to know whether function parameter declaration is a variable, then this should be a separate question. Or reformulate this question asking if there are other cases when a variable doesn't have name, except in `catch` clause. – Language Lawyer Jun 23 '20 at 15:57
  • @LanguageLawyer It's like starting a ship into the dark sea in 16th century. You don't know exactly where you will come to at the start of the journey. Just like in life. I'd have done this, if I knew better, indeed. But I didn't knew it would be only a thing for the parameter variable declaration. – RobertS supports Monica Cellio Jun 23 '20 at 16:00
  • I think after your question has been flagged as dup, it is fine to slightly change it so it is not looks like a dup and the existing answer is still apply. – Language Lawyer Jun 23 '20 at 16:02
  • @LanguageLawyer I did. I don't know if it is sufficient, but it's different. The other question asks for what a variable is itself: "*So, is variable now just a hypernym for object and reference, whether named or not?*" while my question is about "*How and where it is possible that a variable has no associated name?*". It's subtle, but different. Only couples of hair between, but different. – RobertS supports Monica Cellio Jun 23 '20 at 16:20
  • There's a reason cppreference.com/book has a hideous deprecation warning box on every page. – T.C. Jun 24 '20 at 12:35
  • @T.C. Maybe it was wrong to cite any reference except the standard... I'll remember that. But since it was matching with my knowledge at this point of time, I've felt it would strengthen the expression I wanted to give and show what I'd ask for but it seems it were a complete flop. :-/ Anyway, I'll try to keep the reference for now in the question. – RobertS supports Monica Cellio Jun 24 '20 at 12:50

1 Answers1

7

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

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • How can I access this object without name in this case? – RobertS supports Monica Cellio Jun 23 '20 at 15:14
  • 1
    @RobertS you can't since it doesn't have a name. – Rakete1111 Jun 23 '20 at 15:15
  • And is this valid for function definitions as well as function declarations/prototypes? I come from C, where we are only allowed to omit the name at the function declaration/prototype. Yes, I know C and C++ are different. But just to mark my point of knowledge at the moment. – RobertS supports Monica Cellio Jun 23 '20 at 15:16
  • 4
    @RobertSsupportsMonicaCellio [parameter name omitted, C++ vs C](https://stackoverflow.com/questions/8776810/parameter-name-omitted-c-vs-c) – François Andrieux Jun 23 '20 at 15:17
  • @Rakete1111 So a variable and an `int` object in memory is defined, but I can't access it? Is this the one and only case where a variable does not need to have a name or are there other cases beside? – RobertS supports Monica Cellio Jun 23 '20 at 15:32
  • And we can just write `void foo(int&)` to demonstrate it for references, assuming that's the kind of "references" the cited passages are referring to – Asteroids With Wings Jun 23 '20 at 15:32
  • 1
    @RobertSsupportsMonicaCellio Correct and personally I can't think of another example off the top of my head – Asteroids With Wings Jun 23 '20 at 15:32
  • 1
    though anonymous class/union/enum-type members may qualify maybe perhaps – Asteroids With Wings Jun 23 '20 at 15:32
  • 1
    @RobertS Exactly. Sure like exception handler parameters. I don't think there are others, but I might be wrong. – Rakete1111 Jun 23 '20 at 15:35
  • @Rakete1111 And there purpose of that is what Keith said in [his answer](https://stackoverflow.com/a/8777073/12139179): "*In C++, if the function is derived from a function defined in some parent class, it has to have the same signature as the parent, even if the child function has no use for one of the parameter values.*", right or is there another purpose? – RobertS supports Monica Cellio Jun 23 '20 at 15:51
  • 2
    @RobertS yes exactly. Here's another: ` = delete;`d functions are definitions and for them it might make sense to not name the parameters. – Rakete1111 Jun 23 '20 at 16:14
  • 1
    or some functor that is required (by e.g. an algorithm) to accept a specific param list that you might not be interested in for some particular implementation – Asteroids With Wings Jun 24 '20 at 11:41