Torben L.
Torben L.

Reputation: 191

VS2013 - Error with multiple includes of same header

While porting a project from Visual Studio 2005 to 2013, I came across this strange behaviour for which I cannot find an explanation. The context was about creating template specializations by including a certain header file multiple times, but changing preprocessor definitions before each include to basically generate a different class declaration.

I could narrow down the issue 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. both Foo and Bar are declared and usable in main().

Now, to cause the issue, change the #ifdef in the line marked by (1) to #ifndef, which should effectively only cause the order in which Foo and Bar are declared to be swapped. But instead, compilation then fails:

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

The preprocessed file looks like this (stripped some whitespace):

#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;
}

My question is: Am I missing something? Is this expected behaviour for some reason? Is it a compiler setting/bug that makes Visual Studio skip the header contents (including the #else part) a second time when it thinks it has a header guard (because of the #ifndef)?

Thanks!

Upvotes: 4

Views: 211

Answers (1)

LThode
LThode

Reputation: 1963

This is MS Connect issue 800200 as per dyp's comment, and was fixed in VS2013 RTM.

Upvotes: 1

Related Questions