C预处理器函数宏与空的aguments [英] C preprocessor function macros with empty aguments
问题描述
在尝试将一些东西从Unix移植到Windows时,我遇到了一个带有空参数的函数宏的奇怪行为。这是一个小的
片段,说明了问题:
#include< iostream>
#include< string>
使用命名空间std;
#define B(X,Y)Y
int main()
{
string mystr(B(,hello world));
cout<< mystr<< std :: endl;
返回0;
}
这正确打印''hello world''用g ++编译时。但是,这个
程序无法在VC ++上编译。在VC ++中经过预处理器输出
后,我发现宏调用行是:
string mystr();
这个扩展是不正确。
g ++做正确的扩展(记录如下:
http://gcc.gnu.org/onlinedocs/gcc-3....p_3.html#SEC15 )
并且g ++预处理器的相同语句的输出是:
string mystr(" hello world");
稍微摆弄一下,我意识到VC ++预处理器
向左移动了传递的非空参数,如果有的话。例如,在
上将宏定义更改为:
#define B(X,Y)XX
语句字符串mystr(B( ,你好世界));得分不正确
在VC ++上扩展为
string mystr(hello world" hello world");
而g ++正确扩展它to
string mystr();
我使用的是VC ++ 6.0
- Saby
Sabyasachi Basu写道:
这正确打印''你好世界''用g ++编译时。但是,这个
程序无法在VC ++上编译。
那你为什么不用g ++编译呢?你也可以在Windows中使用它来
来编译windows二进制文件。你可以从这里得到答案:
http://www.mingw。 org /
Sabyasachi Basu写道:
int main()
{
string mystr(B(,hello world));
^^^
不作为参数传递导致未定义的行为,因此两个预处理器都不是正确的。在C99中,这种行为是明确定义的,或者b $ b或多或少的工作就像你假设一个空参数应该有效,但这是
还没有C ++的一部分。
问候,
Paul Mensonides
Paul Mensonides写道:Sabyasachi Basu写道:
int main()
{/>字符串mystr(B(,hello world) );
^^^
将任何内容作为参数传递会导致未定义的行为,因此预处理器都不是正确的。在C99中,这种行为是明确的,并且或多或少的工作就像你假设一个空的参数应该有效,但这还不是C ++的一部分。
问候,
Paul Mensonides
我猜OP可以使用:
#define EMPTY
// yada yada yada
string mystr(B(EMPTY,Hello World));
While trying to port some stuff from Unix to Windows, I encountered a
strange behaviour of function macros with empty arguments. Here is a small
snippet which illustrates the problem:
#include <iostream>
#include <string>
using namespace std;
#define B(X, Y) Y
int main()
{
string mystr (B(, "hello world"));
cout << mystr << std::endl;
return 0;
}
This correctly prints ''hello world'' when compiled with g++. However, this
program fails to compile on VC++. On going through the preprocessor output
in VC++, I found that the macro call line is:
string mystr ();
This expansion is incorrect.
g++ does the correct expansion (as documented:
http://gcc.gnu.org/onlinedocs/gcc-3....p_3.html#SEC15 )
and the output for the same statement for the g++ preprocessor is:
string mystr ("hello world");
On fiddling around a little more, I realised that the VC++ preprocessor
shifts to the left the non-empty arguments passed, if any. For instance, on
changing the macro definition to:
#define B(X, Y) X X
the statement string mystr (B(, "hello world")); got incorrectly
expanded on VC++ to
string mystr ("hello world" "hello world");
while g++ correctly expanded it to
string mystr ( );
I am using VC++ 6.0
-- Saby
Sabyasachi Basu wrote:
This correctly prints ''hello world'' when compiled with g++. However, this
program fails to compile on VC++.
So why don''t you compile it with g++ then? You can use that in Windows
also to compile windows binaries. You can get mingw from here:
http://www.mingw.org/
Sabyasachi Basu wrote:
int main()
{
string mystr (B( , "hello world"));
^^^
Passing nothing as an argument results in undefined behavior, so neither
preprocessor is "correct." In C99, this behavior is well-defined and
more-or-less works like you''d assume an empty argument should work, but this is
not yet a part of C++.
Regards,
Paul Mensonides
Paul Mensonides wrote:Sabyasachi Basu wrote:int main()
{
string mystr (B( , "hello world"));
^^^
Passing nothing as an argument results in undefined behavior, so neither
preprocessor is "correct." In C99, this behavior is well-defined and
more-or-less works like you''d assume an empty argument should work, but this is
not yet a part of C++.
Regards,
Paul Mensonides
I guess the OP could use:
#define EMPTY
// yada yada yada
string mystr(B(EMPTY, "Hello World"));
这篇关于C预处理器函数宏与空的aguments的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!