g++ 4.7 evaluates operator "" as sibling to macro expansion
Asked Answered
P

1

13

I'm moving some code over to GCC 4.7 (from 4.6) and ran into a few compiler errors and found the problem documented in the GCC 4.7 porting guide:

User-defined literals and whitespace

The C++ compiler in ISO C11 mode std={c++11,c++0x,gnu++11,gnu++0x} supports user defined literals, which are incompatible with some valid ISO C++03 code.

In particular, whitespace is now needed after a string literal and before something that could be a valid user defined literal. Take the valid ISO C++03 code

const char *p = "foobar"__TIME__;

In C++03, the TIME macro expands to some string literal and is concatenated with the other one. In C++11 __TIME__ isn't expanded, instead operator "" __TIME__ is being looked up, resulting in the following diagnostic:

error: unable to find string literal operator  ‘operator"" __TIME__’

This applies to any string literal followed without whitespace by some macro. To fix, just add some whitespace between the string literal and the macro name.

While I could fix the errors, I'd like to know why I must do this. __TIME__ is a macro, so "something"__TIME__ would change into "something""15:52:03" (or similar) in the preprocessing stage, so the compiler would never have the opportunity to see it as operator "".

Is this behavior standards-approved or is it a bug?

Platform answered 10/8, 2012 at 21:58 Comment(4)
"xxx"yyy is a single token in C++11, while it was two seperate token in C++03, which means the __TIME__ macro is never detected as such.Alignment
Just recently discussed: #11870093Enloe
This is how user-defined literals work (as mentioned in the docs you quoted). The C++11 standard specifically calls this out as a breaking change (Appendix C.2.1). If you want to compile your code with a C++11 compiler mode, you'll need to change your source to fix the breaking change that C++11 made. If you don't want to change your source code, I think you'll have to tell g++ to use -std=c++98 or -std=gnu++98.Dubitation
Looks like there is a documentation bug, it should say "The C++ compiler in ISO C++11 mode", since C11 is a very different beast. The new behavior of the actual compiler is correct though.Wig
B
16

The issue is that "foobar"__TIME__ is no longer tokenized into the preprocessor tokens "foobar" followed by __TIME__.

Preprocessor tokens "have the lexical form of a keyword, an identifier, a literal, an operator, or a punctuator." The addition of user defined literals changes what is lexed as a preprocessor token. Now "foobar"__TIME__ is a single user-defined-character-literal preprocessor token, and so when phase 4 of translation occurs, which would replace __TIME__ with the "15:52:03", there is no __TIME__ token to get replaced this way.

Yes, this behavior is specified in the standard.

Due to the cinttypes macros it looks like more code is affected by this than the committee realized and they are considering addressing it. Some compilers are already moving to handle problems with cinttypes, however not in a way that would fix this use of __TIME__ for you. I think your best bet is to just change the code.

Brannen answered 10/8, 2012 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.