Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ gcc与visualstudio宏扩展_C++_Visual Studio 2010_Gcc_C Preprocessor - Fatal编程技术网

C++ gcc与visualstudio宏扩展

C++ gcc与visualstudio宏扩展,c++,visual-studio-2010,gcc,c-preprocessor,C++,Visual Studio 2010,Gcc,C Preprocessor,给定以下代码: void doSomething(int one, int two, int three) { //something here } #define ONE 1,2,3 #define TWO(arg) doSomething(arg); #define THREE(arg) TWO(arg) void doSomethingElse() { TWO(ONE) THREE(ONE) } visual studio 2010具有以下预处理器输出(省略一

给定以下代码:

void doSomething(int one, int two, int three)
{
   //something here
}

#define ONE 1,2,3

#define TWO(arg) doSomething(arg);

#define THREE(arg) TWO(arg)

void doSomethingElse()
{
   TWO(ONE)

   THREE(ONE)
}
visual studio 2010具有以下预处理器输出(省略一些空行):

而gcc 4.2给出了以下内容:

void doSomething(int one, int two, int three)
{

}    

void doSomethingElse()
{
   doSomething(1,2,3);

myFile.cpp:17:13: error: macro "TWO" passed 3 arguments, but takes just 1 
   TWO
}

我不确定哪个是标准的,但我希望它能像VisualStudio一样工作。有没有办法重构代码,使其在两个编译器中都能以这种方式工作?

在另一个宏中使用逗号时,需要进行特殊处理

这应该起作用:

#define ONE() 1,2,3
#define TWO(ARG) doSomething(ARG());
#define THR(ARG) TWO(ARG)
通过将一个转换为类似宏本身的函数,可以延迟它立即被替换


如果

上的,您可以看到更多使用BOOST\u PP\u COMMA\u避免这种情况的示例。

另一种可能是将参数插入括号中,使其在替换中不会变成3个参数:

#define ONE 1,2,3                                                                                                                                                                                                                                                              
#define TWO_PARENS(arg) doSomething arg;                                                                                                                                                                                                                                       
#define TWO(arg) TWO_PARENS((arg));                                                                                                                                                                                                                                           
#define THREE(arg) TWO_PARENS((arg))
THREE(ONE)          

BTW GCC根据St.< /P>是正确的,通常当VS和GCC不同时,GCC是正确的。这是VisualC++预处理器中已知的错误:在重新扫描之前它不扩展宏。gcc的行为是正确的。感谢所有的回复……就所提供的解决方案而言,我认为我过于简化了我的示例,无法让它们工作。我找到了另一个解决方法,但它不能说明这个一般问题,所以我将在这里省略它。

#define ONE 1,2,3                                                                                                                                                                                                                                                              
#define TWO_PARENS(arg) doSomething arg;                                                                                                                                                                                                                                       
#define TWO(arg) TWO_PARENS((arg));                                                                                                                                                                                                                                           
#define THREE(arg) TWO_PARENS((arg))
THREE(ONE)