What should happen when the return value from a C++ function that returns a reference of an undefined object type is not assigned?

Viewed 323

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.

2 Answers
Related