Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: is return value a L-value?

Consider this code:

struct foo {   int a; };  foo q() { foo f; f.a =4; return f;}  int main() {   foo i;   i.a = 5;   q() = i; } 

No compiler complains about it, even Clang. Why q() = ... line is correct?

like image 866
John Avatar asked May 24 '11 14:05

John


People also ask

What is the return value of C?

Example ProgramThe default return value for an integer type is 0. If you do not insert return 0 or any other value in your main() function a, 0 will be returned automatically.

What is return type in C?

Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name − This is the actual name of the function.

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.

What is R value and L value in C?

An lvalue (locator value) represents an object that occupies some identifiable location in memory (i.e. has an address). rvalues are defined by exclusion. Every expression is either an lvalue or an rvalue, so, an rvalue is an expression that does not represent an object occupying some identifiable location in memory.


2 Answers

No, the return value of a function is an l-value if and only if it is a reference (C++03). (5.2.2 [expr.call] / 10)

If the type returned were a basic type then this would be a compile error. (5.17 [expr.ass] / 1)

The reason that this works is that you are allowed to call member functions (even non-const member functions) on r-values of class type and the assignment of foo is an implementation defined member function: foo& foo::operator=(const foo&). The restrictions for operators in clause 5 only apply to built-in operators, (5 [expr] / 3), if overload resolution selects an overloaded function call for an operator then the restrictions for that function call apply instead.

This is why it is sometimes recommended to return objects of class type as const objects (e.g. const foo q();), however this can have a negative impact in C++0x where it can inhibit move semantics from working as they should.

like image 141
CB Bailey Avatar answered Oct 15 '22 12:10

CB Bailey


Because structs can be assigned to, and your q() returns a copy of struct foo so its assigning the returned struct to the value provided.

This doesn't really do anything in this case thought because the struct falls out of scope afterwards and you don't keep a reference to it in the first place so you couldn't do anything with it anyway (in this specific code).

This makes more sense (though still not really a "best practice")

struct foo {   int a; };  foo* q() { foo *f = new malloc(sizeof(foo)); f->a = 4; return f; }  int main() {   foo i;   i.a = 5;    //sets the contents of the newly created foo   //to the contents of your i variable   (*(q())) = i; } 
like image 29
Chad Avatar answered Oct 15 '22 12:10

Chad