define and single quotes and C++11 vs 14

Xi Ruoyao ryxi@stu.xidian.edu.cn
Tue Feb 7 02:44:00 GMT 2017


On 2017-02-06 Mon 21:49 +0000, Jason Mancini wrote:

> Is this the expected behavior? 

Yes. According to C++ Standard (ISO/IEC 14882:2011 2.5):

     A preprocessing token is the minimal lexical element of the
     language in translation phases 3 through 6. The categories of
     preprocessing token are: header names, identifiers, ....., and
     single non-white-space characters that do not lexically match the
     other preprocessing token categories. If a ' or a " character matches
     the last category, the behavior is *undefined*.

Then everything may happen.

> There doesn't seem to be a selective -Wno-* flag for this warning. 

It it a preprocessor warning. We can suppress preprocessor warnings by

    g++ foo.cc -std=c++11 -Wp,-w -no-integrated-cpp

> C++11 mode rejects the code, while C++14 mode accepts the code, and
> g++ -E shows Q to be 1'b0 in that case.

> source code:
> ----------------------
> #define Q 1'b0
> ----------------------
> 
> # g++-6.3 -Werror -c src.cc -std=c++14

This is because that, in C++14, "'" may be digit separators. The GCC
preprocessor can not detect "digit separator outside digit sequence"
at now. So there is no warnings. Maybe we should make an
enhancement to add this warning.

> # g++-6.3 -Werror -c src.cc -std=c++11
> error: missing terminating ' character [-Werror]

In C++11, "'" can only indicate a character literal. GCC preprocessor
can easily detect "missing terminating ' character".

-- 
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University



More information about the Gcc-help mailing list