Are temporary objects in C ++ const?

I always thought that temporary objects in C ++ are automatically treated as const by the compiler. But recently, I experienced that the following code example:

function_returning_object().some_non_const_method();

valid for C ++ compiler. And this makes me wonder - are temporary objects really const in C ++? If so, why is the code above considered the right compiler?

+4
source share
6 answers

No, it is not. Not unless you declare the return type as const.

+6
source

It depends.

 int f(); const int g(); class C { }; C x(); const C y(); 

In the case of both f() and g() return value is not constant, because there are no constant values โ€‹โ€‹of type non-class. const in the returned g() completely useless (in fact, it is worse than useless, since it can in rare cases cause problems with creating a template).

In the case of x() return value is not constant (because it is not constant). In the case of y() return value is constant (because the return type is const-qualified). The const separator here (or lack thereof) makes sense since the type of the return type is the type of the class.

+5
source

When answering the question at first, they are actually not constants. You cannot bind it to a non-constant link. This was probably done to prevent errors in certain situations when they were passed as a parameter to a function that modifies them, only to make changes to a temporary object, and not for the intended purpose.

Allowing non-constant operations in temporary mode is especially useful if you want to call "swap" on it with a local variable.

 std::vector<T> local; method_that_returns_a_vector().swap( local ); 

Until the semantics of semantic days, this was considered the most efficient way to return a large data set and retrieve it without copying all the data.

+5
source

Temporary objects can be const, but they do not have to be.

 ((string const)"hello").append(" world"); // error! 

It allows different things. Consider

 struct bitref { int index; bitref &operator=(bool value); // non-const! }; struct bitset { int flags; // returns a bitref temporary that associated with the bit // at index 'index'. bitref operator[](int index); // ... }; 

You could do

 bitset b; b[1] = true; // change second bit to 1 

This is what is done using the std::bitset<> template.

+2
source

Temporary objects are not constants, but they can only be bound to lvalue constants. It is easy to demonstrate that the inclusion of time references to non-constant links will be applied in almost all scenarios. You also cannot accept the temporary address, even if you can link the link to it, as well as a number of other very stupid things related to temporary in C ++ 03. I'm just glad that C ++ 0x will be soon ... I hope.

+2
source

It all depends on the type of function returned.

 //Temporary objects: nameless objects that are only usable in current statement Object function(); //Return a temporary object by value (using copy constructor) const Object function(); //Return a const temp object by value //references, return a reference to an object existing somewhere else in memory Object & function(); //Return an object reference, non-const behaves as any other non-const const Object & functon(); //Return const obj reference, behaves as any other const 
0
source

Source: https://habr.com/ru/post/1335676/


All Articles