[英]Weird g++ preprocessor behavior in c++11 mode
I have the problem that when g++ is run in c++11 mode, some proprocessor macros are not expanded correct. 我有一个问题,当g ++在c ++ 11模式下运行时,某些预处理器宏未正确扩展。 This causes me troubles during the compilation of programs using Qt.
这使我在使用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.
The following snipped exposes the problem: 以下剪辑揭示了问题:
$ 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)
Preprocesing it without c++11 does the right thing. 在没有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")
But in C++11 mode the QTOSTRING macro does not get expanded, causing a compile error at the source line. 但是在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))
Is this behavior intended, and what can I do to enable the expansion? 这种行为是否有意,我该怎么做才能启用扩展?
This is a known problem and the new GCC behaviour is intentional as a result of a new C++11 feature, namely user-defined literals. 这是一个已知问题,新的GCC行为是有意的,因为新的C ++ 11特性,即用户定义的文字。 You can insert a space before
__FILE__
and QTOSTRING
to ensure it will always be treated as a separate token and thus expanded. 您可以在
__FILE__
和QTOSTRING
之前插入一个空格,以确保它始终被视为一个单独的标记,从而进行扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.