在将项目从Visual Studio 2005移植到2013时,我遇到了这种奇怪的行为,无法找到解释。上下文是关于通过多次包含一个特定的头文件来创建模板专业化,但是要在每个包含之前更改预处理器定义,以基本上生成不同的类声明。
我可以将问题缩小到以下情况:
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;
}
这可以按预期工作,即Foo
和Bar
都可以在中声明和使用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
),使Visual Studio第二次跳过标题内容(包括该部分)?
谢谢!
根据dyp的评论,这是MS Connect问题800200,已在VS2013 RTM中修复。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句