Real behavior of g ++ preprocessor in C ++ 11 mode

I have a problem when g ++ starts in C ++ 11 mode, some proprocessor macros are not expanded correctly. This causes me problems compiling programs using 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 snapshot reveals 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) 

Pre-execution without C ++ 11 does the right thing.

 $ 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 expand, causing a compilation error in the source line.

 $ 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 extension?

+6
source share
1 answer

This is a known issue, and the new GCC behavior is intentional as a result of using the new C ++ 11 function, namely user literals. You can insert a space before __FILE__ and QTOSTRING to ensure that it will always be treated as a separate token and thus be extended.

QT bugreport here .

+9
source

Source: https://habr.com/ru/post/944356/


All Articles