Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 - i.e. I will always get { 0, '1', '2', '3', ... }

like image 336
ezpz Avatar asked May 14 '10 20:05

ezpz


2 Answers

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.

like image 117
AnT Avatar answered Sep 24 '22 02:09

AnT


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.

like image 27
James McNellis Avatar answered Sep 21 '22 02:09

James McNellis