Re: A non-const reference may only be bound to an lvalue?
- From: George <George@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 15 Dec 2007 06:30:01 -0800
Hi Doug,
To my surprise, I have tested that the following code can compile,
struct X {
};
void f(X& x) {};
void g()
{
f(X());
}
I think it breaks the rule that non-const reference parameter X& x can not
be binded to a rvalue (return of X(), which is a temporary object, i.e.
rvalue)? Any comments?
I am using Visual Studio 2008.
"Doug Harrison [MVP]" wrote:
On Fri, 14 Dec 2007 08:56:53 -0500, "Igor Tandetnik" <itandetnik@xxxxxxxx>
wrote:
To avoid surprises like this:
void f(long& x) { x = 1; }
int y = 0;
f(y); // hypothetical, doesn't compile
assert(y == 1); // fails
If f(y) were allowed to compile, it would generate a temporary of type
long, and the function would modify that temporary, leaving the original
int variable unchanged.
It would also mean that a seemingly benign change in function
signature( from f(int&) to f(long&) ) may silently alter the behavior of
the program. As things stand now, such a change would lead to compiler
errors - much better than silent behavior change.
The thing is, that's got little to do with the typical desired usage, which
is to permit:
void f(X&);
void g()
{
f(X());
}
There are legitimate reasons to want to do that, but I've come to accept it
shouldn't be allowed for this reason. Suppose you have a class:
struct Y
{
// Lifetime of x must exceed object's use of it
explicit Y(X& x)
: m_x(x)
{
}
X& m_x;
};
Whether or not you agree with the design of the class, under the existing
rule, you can't get into trouble binding a temporary to m_x, at least not
easily. And when you really need to say f(X()), there's always lvalue_cast.
--
Doug Harrison
Visual C++ MVP
regards,
George
.
- References:
- Re: A non-const reference may only be bound to an lvalue?
- From: Carl Daniel [VC++ MVP]
- Re: A non-const reference may only be bound to an lvalue?
- From: George
- Re: A non-const reference may only be bound to an lvalue?
- From: Igor Tandetnik
- Re: A non-const reference may only be bound to an lvalue?
- From: George
- Re: A non-const reference may only be bound to an lvalue?
- From: Igor Tandetnik
- Re: A non-const reference may only be bound to an lvalue?
- From: Doug Harrison [MVP]
- Re: A non-const reference may only be bound to an lvalue?
- Prev by Date: Re: A non-const reference may only be bound to an lvalue?
- Next by Date: Re: non-const reference and const reference
- Previous by thread: Re: A non-const reference may only be bound to an lvalue?
- Next by thread: Re: A non-const reference may only be bound to an lvalue?
- Index(es):
Relevant Pages
|