简体   繁体   中英

C++ Preprocessor string literal concatenation

I found this regarding how the C preprocessor should handle string literal concatenation (phase 6). However, I can not find anything regarding how this is handled in C++ (does C++ use the C preprocessor?).

The reason I ask is that I have the following:

const char * Foo::encoding = "\0" "1234567890\0abcdefg";

where encoding is a static member of class Foo . Without the availability of concatenation I wouldnt be able to write that sequence of characters like that.

const char * Foo::encoding = "\01234567890\0abcdefg";

Is something entirely different due to the way \\012 is interpreted.

I dont have access to multiple platforms and I'm curious how confident I should be that the above is always handled correctly - ie I will always get { 0, '1', '2', '3', ... }

The language (C as well as C++) has no "preprocessor". "Preprocessor", as a separate functional unit, is an implementation detail. The way the source file(s) is handled if defined by so called phases of translation . One of the phases in C, as well as in C++ involves concatenating string literals.

In C++ language standard it is described in 2.1. For C++ (C++03) it is phase 6

6 Adjacent ordinary string literal tokens are concatenated. Adjacent wide string literal tokens are concatenated.

Yes, it will be handled as you describe, because it is in stage 5 that,

Each source character set member and escape sequence in character constants and string literals is converted to the corresponding member of the execution character set (C99 §5.1.1.2/1)

The language in C++03 is effectively the same:

Each source character set member, escape sequence, or universal-character-name in character literals and string literals is converted to a member of the execution character set (C++03 §2.1/5)

So, escape sequences (like \\0 ) are converted into members of the execution character set in stage five, before string literals are concatenated in stage six.

Because of the agreement between the C++ and C standards. Most, if not all, C++ implementations use a C preprocessor, so yes, C++ uses the C preprocessor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM