Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions returning lvalues, always return lvalue references

Tags:

c++

Scott meyers in item 3 of effective c++ says

Applying decltype to a name yields the declared type for that name. Names are typically lvalue expressions, but that doesn’t affect decltype’s behavior. For lvalue expressions more complicated than names, however, decltype generally ensures that the type reported is an lvalue reference. That is, if an lvalue expression other than a name has type T, decltype reports that type as T&. This seldom has any impact, because the type of most lvalue expressions inherently includes an lvalue reference qualifier. Functions returning lvalues, for example, always return lvalue references.

What does it mean when he says Functions returning lvalues, for example, always return lvalue references

like image 875
user3819404 Avatar asked Nov 16 '20 17:11

user3819404


People also ask

Can a function return an lvalue?

Why is it not possible to set a function returning an address while it is possible to set a function that returns a reference. Short answer: You return a pointer by-value and anything returned by-value is (by definition) not an lvalue.

Are references Lvalues?

“l-value” refers to a memory location that identifies an object. “r-value” refers to the data value that is stored at some address in memory. References in C++ are nothing but the alternative to the already existing variable.

What are Lvalues in C++?

An lvalue is an expression that yields an object reference, such as a variable name, an array subscript reference, a dereferenced pointer, or a function call that returns a reference. An lvalue always has a defined region of storage, so you can take its address.

What are Rvalues and Lvalues?

An lvalue refers to an object that persists beyond a single expression. An rvalue is a temporary value that does not persist beyond the expression that uses it.


1 Answers

It means what it says!

There is no way to make a function, such that its return type is not T&, yet calling it results in an lvalue expression.

Every other return type results in the function call being an rvalue expression.

This is intuitive when you consider that the only way to "return" something that already exists, is by reference — the result of a function call is otherwise always a "temporary", whether because it's copying some local variable, or because it's moving from some local variable.

The would-be exception to this rule, returning T&&, doesn't apply either because these produce rvalue expressions (which is what makes move semantics work, since only rvalue expressions can go on to bind to T&& parameters).

Scott is reporting a consequence of the rules of the language, and telling us that the same consequence was used as a justification for one of the rules of decltype.

Arguably, he could have more clearly phrased it thus:

The only functions whose calls evaluate to lvalues, are those that return lvalue references.

like image 57
Asteroids With Wings Avatar answered Oct 16 '22 10:10

Asteroids With Wings