Muhammad
Muhammad

Reputation: 1665

error: invalid preprocessing token

#define A(a)  "str" ## a ## test
A(_)

According to 17.6.4.3.5 in C++11 standard

Literal suffix identifiers that do not start with an underscore are reserved for future standardization.

the above code should produce "str"_test which is a valid preprocessing token and it's class user-defined-string-literal.

clang 3.0 produce error when running in preprocessor mode via -E.

clang gives:

pasting formed '"str"_', an invalid preprocessing token
A(_)
^
note: expanded from:
#define A(a)  "str" ## a ## test
                    ^

"str"_test
1 error generated.

i don't understand what steps made it decide that the result is not a invalid preprocessing token.

Note: I'm writing a c++11 preprocessor.

Upvotes: 4

Views: 6213

Answers (1)

Keith Thompson
Keith Thompson

Reputation: 263267

I think the code is valid C++11; it looks like you're using a compiler with incomplete C++11 support.

Using g++ version 4.7.2 (with -std=c++11), this contrived program:

#include <cstddef>
#include <iostream>

#define A(a)  "str" ## a ## test

const char* operator"" _test(const char s[4], size_t size) {
    return s;
}

int main() {
    std::cout << A(_) << "\n";
}

compiles without error and produces this output:

str

clang++ version 3.0 is less happy; among other errors, it says:

c.cpp:11:18: error: pasting formed '"str"_', an invalid preprocessing token
    std::cout << A(_) << "\n";
                 ^
c.cpp:4:21: note: expanded from:
#define A(a)  "str" ## a ## test
                    ^

Upvotes: 3

Related Questions