When I compile your code with:
g++ foo.cpp main.cpp -o main
I get:
main.cpp: In function 'int main()': main.cpp:12:52: error: invalid use of incomplete type 'struct foo' main.cpp:4:8: error: forward declaration of 'struct foo'
This is consistent with my interpretation of the standard that you cannot apply typeid to an incomplete type - and a_ref_foo is of an incomplete type, since the full definition of the type foo not displayed. main.cpp (with lines added) is poorly formed and requires diagnostics.
Update:
I reproduced this problem using Visual Studio 2010 Express. Even with language extensions disabled, this trivial program:
#include <typeinfo> struct foo; int main() { typeid (foo); return 0; }
compiled without diagnostic messages. With gcc 4.7, I get:
main.cpp: In function 'int main()': main.cpp:7:14: error: invalid use of incomplete type 'struct foo' main.cpp:3:8: error: forward declaration of 'struct foo'
The same rule:
If the type of the expression is the type of the class, the class must be fully defined.
appears in versions of the ISO C ++ standard 1998, 2003 and 2012.
Looks like a bug in Visual Studio. (If anyone wants to report this to Microsoft, continue.)
Keith thompson
source share