Do preprocessor defines substitute in `operator""_name`

394 Views Asked by At

Consider the following example provided by Aykhan Hagverdili:

#include <string>

using std::operator""s;

#define s foobar

auto s = "hello world"s;

Some compilers would substitute s and fail compilation. Some compilers would not substitute s.

See the result here: https://godbolt.org/z/jx4nhYczd gcc fails, clang compiles

Which is right?

2

There are 2 best solutions below

0
user17732522 On BEST ANSWER

"hello world"s is, since C++11, a single preprocessing token matching the user-defined-string-literal grammar production.

Macros substitute on the level of preprocessing tokens. Since there is no s preprocessing token, there can't be any substitution of your macro.

As a result your code is well-formed in C++14 or later. std::operator""s is chosen. In C++11 there was no std::operator""s.

However, before C++11 when user-defined literals were introduced, "hello world"s would have been two preprocessing tokens "hello" and s, so the latter one would have been substituted with your macro.

This is even explicitly mentioned in [diff.cpp03.lex]/2 as a backwards-incompatible change with C++11.

0
n. m. could be an AI On

GCC is wrong.

A user-defined string literal like "hello world"s is a preprocessing-token, not two preprocessing-tokens "hello world" and s. The preprocessor should not replace parts of a preprocessing-token.