Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can temporaries bind to non-const references?

I wrote the following code to test this:

struct X
{
   char* x;
   X()
   {
      x = new char('a');
   }
   ~X()
   {
      *x = 'b';
      delete x;
   }
};

void foo(const X& x)
{
}
void goo(X& x)
{
}

int main()
{
   foo(X());
   goo(X());
}

The destructors for the temporaries are called after both functions exit, but I thought you can only bind a temporary to a const reference. Why does goo work then?

Is it UB and MSVS is wrong, or is it ok?

like image 860
AMCoder Avatar asked Oct 08 '22 19:10

AMCoder


1 Answers

It's illegal. A conforming implementation diagnoses it (i.e. it must at least warn), but MSVC++ allows it as an extension.

Or a bug if you're unkind, but IIRC the reason they still allow it is for long-lived legacy reasons: nominally to support code written for MSVC++ before C++ was standardized, but of course once you allow people to write this, they write it accidentally in new code too, so the legacy lives on. If it's deliberate then it's a (mis-)feature, not a bug, right? Anyway, a conforming implementation is required to diagnose ill-formed programs, so if you don't get a warning then the compiler isn't conforming.

like image 70
Steve Jessop Avatar answered Oct 10 '22 08:10

Steve Jessop