Error: Invalid Preprocessing Token

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

According to 17.6.4.3.5 in the C ++ 11 standard

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

the above code should create a "str"_test , which is a valid preprocessing token, and a class user-defined-string-literal .

clang 3.0 generates an error when starting 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 do not understand what steps have decided that the result is not an invalid pre-processing token.

Note. I am writing a C ++ 11 preprocessor.

+4
source share
1 answer

I think the code is valid C ++ 11; it looks like you are 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 errors and produces this output:

 str 

clang ++ version 3.0 is less happy; among other mistakes, he 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 ^ 
+2
source

Source: https://habr.com/ru/post/1487098/


All Articles