Re: A non-const reference may only be bound to an lvalue?
Hi Doug,
I do not quite understand why the following code can convert rvalue to
lvalue, could you provide more description please?
template<typename T>
inline T&
lvalue_cast(const T& rvalue)
{
return const_cast<T&>(rvalue);
}
"Doug Harrison [MVP]" wrote:
On Fri, 14 Dec 2007 13:02:20 -0500, David Wilkinson <no-reply@effisols.com>
wrote:
Of course it is easily fixed by doing
void g()
{
X x;
f(x);
}
Alternatively, you can use the lvalue_cast I mentioned and keep the
original syntax:
template<typename T>
inline T&
lvalue_cast(const T& rvalue)
{
return const_cast<T&>(rvalue);
}
void g()
{
f(lvalue_cast(X()));
}
Or for some X, you can use the "joke" I posted in the thread Carl linked
to:
f(X() = X()); // Not serious
Only in C++ could assigning one rvalue to another rvalue produce a
modifiable lvalue. (This is actually the default for class types.)
--
Doug Harrison
Visual C++ MVP
regards,
George