What is an anonymous object in C ++?

This question brings me into some confusion. After all, on this request, Google says that there are no anonymous objects in C ++. For example, anonymous classes in c ++ . And then, quote from MSDN :

A lambda expression (or simply lambda ) in C ++ 11 is a convenient way to define an anonymous function object.

  • one
    And you are confusing classes and objects. Objects have no names at all. - VTT
  • @VTT and not tell me where to read on this issue. For apparently I am completely confused. - WierdGreenCat
  • 2
    Classes are types (they can have a user-supplied name, or they can be anonymous). Anonymous - this means that they actually also have a name (generated by the compiler), but it is unknown to the user and cannot be called by name. Objects are instances of some type (they have no names). The name may be for a variable, but not for an object that is stored in this variable. - VTT
  • The question is meaningless without context. Where did you originally take this term - "anonymous object"? Give a complete context, then it becomes clear what was discussed. - AnT pm
  • @AnT A lambda expression (or simply lambda) in C ++ 11 is a convenient way to define an anonymous function object. msdn.microsoft.com/en-us/library/dd293608.aspx - WierdGreenCat pm

2 answers 2

Perhaps the following is not what you need, but I will go over the terminology.

There is no direct term "anonymous object" in C ++. But there is an "unnamed object" (unnamed object). This term in this form is found in the draft of the standard n4750 only once in the section on "anonymous unions" (anonymous unions) 12.3.1 / 1:

A union of the form union { member-specification } ; is called an anonymous union; it defines an anonymous union object.

An example from the same place with my comments:

 void f() { union { int a; const char* p; }; // определяется безымянный объект типа объединение a = 1; // несмотря на то, что `a` и `p` используются как обычные переменные ... p = "Jennifer"; // ... они, тем не менее, являются членами безымянного объекта } 

But there are other situations, for example, an unnamed object can be obtained by calling a constructor of the form T() or by calling a function that returns a certain type.

 struct T() {}; T f() { T t; return t; } T(); // создаёт безымянный объект f(); // возвращает безымянный объект 

Creating an object through new T is the same as T() , only with placement on the heap, not on the stack. And an infinite lifetime, because if for the pointer that returned new does not cause delete , we will get a memory leak. An unnamed object created on the stack will be destroyed after the closest one ; .

  • And if I understand you then, in this example, the objects are also nameless ideone.com/HRN44x But at the same time they have a type name. But if everything is clear with the first example, the association does not have a type and I don’t understand the name with the second - WierdGreenCat
  • @Nik added new to the answer. - αλεχολυτ

Most likely, we are talking about rvalue

In the c ++ language there is the concept of lvalue and rvalue. If it’s quite simple, lvalue is something that has a name, which means that it’s “nevertheless” can be left in the assignment operator, for example:

 int x; x = 10; 

Here, the x object is an lvalue, and we can assign "to it". It also has the name "x", and the name should not be confused (in this case, "x") and the value (immediately after the definition it is most likely not defined, after assigning the value will be 10). Please note that the "name" in question is present only at the compilation stage. Also, the presence of the name obliges to place the compiler in any memory, which means that the concept of the address of this object (in other words, a pointer) makes sense.

About the rvalue, everything seems to be there. An object that does not have a name is an rvalue, an example:

 int x; x = 10; 

Here 10 is an int rvalue of type if we try to assign something to it: 10 = x; then we get a compilation error. Rvalues ​​can be "right" in an assignment statement, but they cannot be left. The rvalue object exists, but its address does not make sense (it may not exist at all, because, for example, the compiler can place it in a register, or optimize it altogether).

Now about the functions. In the C language, and in C ++ up to 11 standards, if you needed a certain function, you would definitely declare its prototype and description. In the prototype, you gave the function a name. This is an analogue of lvalue for functions, you have a function with a specific name. With 11 standards, it became possible to create lambda functions, these are functions that do not have a name, they can be used in the arguments of other functions, passing as a pointer to a function. It turns out that the lambda function is an anonymous, nameless function.

More details:

  1. About lvalue and rvalue
  2. Anonymous function
  3. Lambda functions in C ++
  4. About lambda functions on cppreference
  • About r / l-value there is a question here. - αλεχολυτ