Re: A non-const reference may only be bound to an lvalue?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: beginners question
    ... > "I'm trying to compile this code ... > int i; ... > void main ... > I tried it with 'struct A' and it worked fine. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: menu-based console application
    ... typedef void; ... struct MenuEntry ... Compile everything, fix typing errors and run the program. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Pointers to functions in structures trubble!
    ... I got it to compile by... ... The & isn't requited and you realy should remove the double underscore ... void; ... struct X x; ...
    (comp.lang.c)
  • Re: CFile and FILE*
    ... ever call exit() in any C++ ... LPVOID, e.g., ... void DoSomething ... Note that you can still compile this as C ...
    (microsoft.public.vc.mfc)
  • Re: Library bug or my fault?
    ... void memcpy ... struct Foo { ... 29 void cp(const Foo *foo) ... You have no definition for cp2so the code above won't compile. ...
    (comp.lang.c)