Re: A non-const reference may only be bound to an lvalue?
- From: George <George@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 15 Dec 2007 06:33:00 -0800
Thanks David,
I agree and understand from you that from Spec we should not bind a rvalue
to a non-const reference, but in Visual Studio 2008, the following code can
compile and no warning messages. Any comments?
struct X {
};
void f(X& x) {};
void g()
{
f(X());
}
"David Wilkinson" wrote:
Doug Harrison [MVP] wrote:
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:
Doug:
Yes, this is the kind of situation where this has bitten me. In my case,
X was a function object with some modifiable internal state that was
used in f, but was not needed in g after f returned. This was
cross-platform code (developed on VC7.1), and just one of the other
compilers we were using a few years ago would not accept it (now, I
think, most will not).
Of course it is easily fixed by doing
void g()
{
X x;
f(x);
}
--
David Wilkinson
Visual C++ MVP
regards,
George
.
- Follow-Ups:
- Re: A non-const reference may only be bound to an lvalue?
- From: Bo Persson
- Re: A non-const reference may only be bound to an lvalue?
- 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?
- From: David Wilkinson
- Re: A non-const reference may only be bound to an lvalue?
- Prev by Date: Re: non-const reference and const reference
- Next by Date: Re: default implementation of assignment operator
- 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
|