I ran into this while compiling some portable code in gcc
. Basically this strange code compiles in Visual studio which really just blows my mind:
class Zebra {int x;};
Zebra goo() {Zebra z; return z;}
void foo(Zebra &x)
{
Zebra y;
x = y;
foo(goo());
}
Visual studio
lets this one fly. gcc
will catch this as a compile error. Interestingly, If you typedef Zebra to int, VC++
will complain. Quite contradictory behavior. Thoughts?
As others said, this is due to Microsoft C++ extension. Though
/Za
flag is not recommended as it can break things.Instead use the
/permissive-
switch for better standards compliancy and you will get healthy errors for these cases. Note that this flag is available since VS 2017.More info is on Visual C++ Team Blog.
This is old extension to Visual Studio, the only reference I could find on the Microsoft site was this bug report: Temporary Objects Can be Bound to Non-Const References, which has the following example code:
One of the responses notes:
This blog post: Visual C++ is so Liberal which covers this extension notes that: