Get lvalue through rvalue

G

George2

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;
}


thanks in advance,
George
 
V

Victor Bazarov

George2 said:
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.

You're confusing the return type of 'X()' and the full expression
that you can see inside the parentheses where 'f' is called.
Any ideas?

Code:
struct X {


};

void f (X& x) {}

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

return 0;
}

The trick here is based on the fact that every temporary of a class
type is a separate object, for which you can call member functions.
The operator= is a member function. What is its usual signature?
Rewrite the expression

f( X() = X() )

in terms of member function calls, and you will hopefully see how
an r-value can be turned into an l-value.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top