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

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



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
.



Relevant Pages

  • Re: convert code written without editor to editor compatible?
    ... > without manually creating a form in Visual Studio. ... > form designer serializes its state into your source code in a specific ... >> public static void Main ... >> void OnOKClick ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cant write text on screen after upgrade to VS2005/Vista
    ... like it should work as you want, if I can assume that the methods of a CDC ... You could try your EXE built using Visual Studio 2003 on Windows XP and run ... void CMfcOglView::LabelNds ... CDC* pDC= GetDC; ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Cannot solve these linker errors, please help
    ... but for the first time I've to write ANSI C code using Visual Studio ... int main ... void dislpay{ ...
    (microsoft.public.dotnet.languages.vc)
  • Re: 821206 - using DEBUG_NEW
    ... > void main ... > i'm using visual studio .net ... And how could the compiler know that the delete you expect ...
    (comp.lang.cpp)
  • Re: Constant array vakue from a function
    ... "David Wilkinson" wrote: ... seems like some member functions from the std class or something like that. ... void f1 ... Every malloc must have a free. ...
    (microsoft.public.vc.language)