Consider the following code:
class Foo;
Foo& CreateFoo();
void Bar()
{
CreateFoo();
}
In Visual Studio this will result in an error C2027 that Foo is an undefined type. In most other compilers it compiles fine. It is only an issue if the return value of CreateFoo is not assigned. If I change the line to:
Foo& foo = CreateFoo();
it compiles fine in Visual Studio. Also if Foo is defined rather than just forward-declared, then it will compile fine with no assignment.
Which should be the correct behavior? Is there anything in the C++ standard that addresses this, or is this something that is left to the implementation? I looked and didn’t see anything that talks about this.
Update:
A bug report has been filed.
This looks like the relevant part of the Standard (section 5.2.2):
Since this function result type is an lvalue reference type, the function call evaluates to an lvalue, and the completeness requirement does not apply.
The code is legal, at least in C++11, which no released version of Visual C++ implements fully.