2

Why does this compile on MS Visual C++?

struct myClass{};

void func(myClass& arg){}

void main() {
   func( myClass() );  // works even though func only takes myClass& 
}                      // (not const myClass&!!)

Does this work on other compilers as well or is this MSVC specific (or even a compiler bug?). I can even get the reference on this rvalue like this:

void func(myClass* arg){}

int main() {
    func( &myClass() ); 
}

This works ONLY on Objects that are temporarily created with a constructor. This wouldn't work with any other rvalue like (myClass() + myClass()) for example..

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
sp1r1tu4l
  • 23
  • 3

2 Answers2

10

It compiles because MSVC has a non-standard compliant "extension" that allows binding non-const references to temporaries.

The first example should not compile on a standards compliant compiler.

In the second example, you are taking the address of a temporary to set the value of a pointer. This should also result in an error.

Clang 3.2 produces:

error: taking the address of a temporary object of type 'Foo' [-Waddress-of-temporary]

while GCC 4.7.3 produces

error: taking address of temporary [-fpermissive]

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • "*you are taking the address of a temporary to set the value of a pointer. This is allowed*" Is it? I don't think so. 5.3.1[expr.unary.op]/3: *The operand shall be an lvalue or a quialified-id*, but a temporary is neither of them. It should be rejected by the compiler. – David Rodríguez - dribeas May 15 '13 at 17:00
  • @DavidRodríguez-dribeas good point, thanks. I was fooled by an old version of GCC. – juanchopanza May 15 '13 at 17:06
0

MSVC 2017 with default project settings (that now include /permissive- flag) yields an error:

error C2664: 'void func(myClass &)': cannot convert argument 1 from 'myClass' to 'myClass &'
note: A non-const reference may only be bound to an lvalue

Even if /permissive- option is not viable for some reason using /W4 will yield a warning:

warning C4239: nonstandard extension used: 'argument': conversion from 'myClass' to 'myClass &'
note: A non-const reference may only be bound to an lvalue

user7860670
  • 35,849
  • 4
  • 58
  • 84