# Thread: Get lvalue through rvalue

1. ## Get lvalue through rvalue

Hello everyone,

I do not know how in the following code, rvalue -- return of X(), could result in a lvalue finally and binded to a non-const reference input parameter of function f.

Any ideas?

Code:
```struct X {

};

void f (X& x) {}

int main()
{
f (X() = X());

return 0;
}```

George

2. I think that's basically a trick question

We could change it to:
Code:
```struct X {

};

void f (X& x) {}

int main()
{
f (X().operator=(X()));

return 0;
}```
So, the copy assignment operator is invoked, and this can handle an rvalue as its argument. Then, it returns a reference, so f() can handle the reference as it is an lvalue. Yet, right after the function is done, the temporary X object is destroyed.

3. Thanks laserlight,

Great! *it returns a reference*, you mean assignment operator returns a reference, which is a lvalue?

1. If yes, how do you know assignment operator returns a reference?
2. It is a const reference or non-const reference?

Originally Posted by laserlight
I think that's basically a trick question

We could change it to:
Code:
```struct X {

};

void f (X& x) {}

int main()
{
f (X().operator=(X()));

return 0;
}```
So, the copy assignment operator is invoked, and this can handle an rvalue as its argument. Then, it returns a reference, so f() can handle the reference as it is an lvalue. Yet, right after the function is done, the temporary X object is destroyed.

regards,
George

4. If we assume that the purpose of the assignment operator is to ASSIGN A COPY of the current object, it can't really return a const reference, can it? To know what the assignment operator returns, you have to look at the declaration of it. Technically, it doesn't have to return a reference at all, and it could, technically be declared as a const reference - you just can't make use of the assignment operator that way - just like many other nonsense variations that you CAN make - it's perfectly possible to convince the compiler you want to divide by zero for example - it just doesn't give you a sensible result.

--
Mats

5. Great! *it returns a reference*, you mean assignment operator returns a reference, which is a lvalue?
Yes.

1. If yes, how do you know assignment operator returns a reference?
Because the C++ Standard states:
If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly.
The implicitly-declared copy assignment operator for a class X will have the form
Code:
`X& X::operator=(const X&)`
2. It is a const reference or non-const reference?
non-const reference, as you can infer from the text of the Standard.

If we assume that the purpose of the assignment operator is to ASSIGN A COPY of the current object, it can't really return a const reference, can it?
I do not understand your reasoning. In fact, I had the impression that if not because of the convention enshrined in the standard, returning a const reference would be better to prevent the kind of weird syntax that George2 used in his example. After all, it does not affect operator chaining since the copy assignment operator would take their arguments by const reference anyway.