VS2013 - Error with multiple inclusions of the same header

When porting a project from Visual Studio 2005 to 2013, I came across this strange behavior for which I cannot find an explanation. The context was to create specialized templates that included some header file several times, but changing the preprocessor definitions before each inclusion basically generates another class declaration.

I could narrow the problem down to the following situation:

gen.hpp

#ifdef ENABLE_GEN

#ifdef GEN_SWAP_ORDER // (1)
   class Foo {};
#else
   class Bar {};
#endif

#endif

main.cpp

#define ENABLE_GEN

#include "gen.hpp"
#define GEN_SWAP_ORDER
#include "gen.hpp"

int main()
{
   Foo foo;
   Bar bar;
}

This works as expected, i.e. how Fooand Bardeclared and used main().

, , #ifdef , (1), #ifndef, , , Foo Bar, . :

1>c:\path\to\main.cpp(10): error C2065: 'Bar' : undeclared identifier
1>c:\path\to\main.cpp(10): error C2146: syntax error : missing ';' before identifier 'bar'
1>c:\path\to\main.cpp(10): error C2065: 'bar' : undeclared identifier

( ):

#line 1 "c:\\path\\to\\main.cpp"

#line 1 "c:\\path\\to\\gen.hpp"

   class Foo {};

#line 8 "c:\\path\\to\\gen.hpp"

#line 10 "c:\\path\\to\\gen.hpp"
#line 4 "c:\\path\\to\\main.cpp"

int main()
{
   Foo foo;
   Bar bar;
}

: - ? - ? / , Visual Studio ( #else) , , (- #ifndef)?

!

+4
1

MS Connect 800200 dyp ​​ RTM VS2013.

+1

All Articles