C++ 怪异的g++;c+中的预处理器行为+;11模式

C++ 怪异的g++;c+中的预处理器行为+;11模式,c++,c++11,g++,C++,C++11,G++,我的问题是,当g++在c++11模式下运行时,某些前处理器宏的扩展不正确。这在使用Qt编译程序时会给我带来麻烦 $ g++ --version g++ (GCC) 4.7.2 Copyright (C) 2012 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILIT

我的问题是,当g++在c++11模式下运行时,某些前处理器宏的扩展不正确。这在使用Qt编译程序时会给我带来麻烦

$ g++ --version
g++ (GCC) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
以下截图暴露了问题:

$ cat foo.cpp
//#include <QtGui>
#define QTOSTRING_HELPER(s) #s
#define QTOSTRING(s) QTOSTRING_HELPER(s)
#ifndef QT_NO_DEBUG
# define QLOCATION "\0"__FILE__":"QTOSTRING(__LINE__)
# define METHOD(a)   qFlagLocation("0"#a QLOCATION)
# define SLOT(a)     qFlagLocation("1"#a QLOCATION)
# define SIGNAL(a)   qFlagLocation("2"#a QLOCATION)
#else
# define METHOD(a)   "0"#a
# define SLOT(a)     "1"#a
# define SIGNAL(a)   "2"#a
#endif

METHOD(grml)
$cat foo.cpp
//#包括
#定义QTOSTRING_辅助对象
#定义QTOSTRING_辅助对象QTOSTRING_辅助对象
#ifndef QT\u否\u调试
#定义QLOCATION“\0”\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
#定义方法(a)qFlagLocation(“0”#a QLOCATION)
#定义插槽(a)qFlagLocation(“1”#a QLOCATION)
#定义信号(a)位置(“2”#a位置)
#否则
#定义方法(a)“0”#a
#定义插槽(a)“1”#a
#定义信号(a)“2”#a
#恩迪夫
方法(grml)
不使用c++11进行预处理是正确的

$ g++ -E foo.cpp
# 1 "foo.cpp"
# 1 "<command-line>"
# 1 "foo.cpp"
# 15 "foo.cpp"
qFlagLocation("0""grml" "\0""foo.cpp"":""15")
$g++-E foo.cpp
#1“foo.cpp”
# 1 ""
#1“foo.cpp”
#15“foo.cpp”
qFlagLocation(“0”grml“\0”foo.cpp“:“15”)
但在C++11模式下,QTOSTRING宏不会展开,从而导致源代码行出现编译错误

$ g++ -std=c++11 -E foo.cpp
# 1 "foo.cpp"
# 1 "<command-line>"
# 1 "foo.cpp"
# 15 "foo.cpp"
qFlagLocation("0""grml" "\0"__FILE__":"QTOSTRING(15))
$g++-std=c++11-E foo.cpp
#1“foo.cpp”
# 1 ""
#1“foo.cpp”
#15“foo.cpp”
qFlagLocation(“0”“grml”“\0”“文件”:“QTOSTRING(15))

这是一个已知的问题,新的GCC行为是由于新的C++11特性(即用户定义的文本)而故意的。您可以在
\uu文件\uu
QTOSTRING
之前插入一个空格,以确保它始终被视为一个单独的令牌并因此展开


.

下次:始终包括实际错误。