Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top