简体   繁体   English

C ++预处理器字符串文字串联

[英]C++ Preprocessor string literal concatenation

I found this regarding how the C preprocessor should handle string literal concatenation (phase 6). 我发现这是关于C预处理器应如何处理字符串文字串联(第6阶段)。 However, I can not find anything regarding how this is handled in C++ (does C++ use the C preprocessor?). 但是,我找不到任何关于如何在C ++中处理它的东西(C ++是否使用C预处理器?)。

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 . 其中encoding是类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. 由于解释了\\012的方式,完全不同的东西。

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', ... } 我无法访问多个平台,我很好奇我有多自信,以上总是处理正确 - 即我总是得到{ 0, '1', '2', '3', ... }

The language (C as well as C++) has no "preprocessor". 语言(C和C ++)没有“预处理器”。 "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. C语言和C ++中的一个阶段涉及连接字符串文字。

In C++ language standard it is described in 2.1. 在C ++语言标准中,它在2.1中描述。 For C++ (C++03) it is phase 6 对于C ++(C ++ 03),它是第6阶段

6 Adjacent ordinary string literal tokens are concatenated. 6连接相邻的普通字符串文字标记。 Adjacent wide string literal tokens are concatenated. 相邻的宽字符串文字标记是连接的。

Yes, it will be handled as you describe, because it is in stage 5 that, 是的,它将按照您的描述进行处理,因为它处于第5阶段,

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) 字符常量和字符串文字中的每个源字符集成员和转义序列都将转换为执行字符集的相应成员(C99§5.1.1.2/ 1)

The language in C++03 is effectively the same: C ++ 03中的语言实际上是相同的:

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) 字符文字和字符串文字中的每个源字符集成员,转义序列或通用字符名称都将转换为执行字符集的成员(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. 因此,在第六阶段连接字符串文字之前,转义序列(如\\0 )将转换为第五阶段中执行字符集的成员。

Because of the agreement between the C++ and C standards. 由于C ++和C标准之间的一致性。 Most, if not all, C++ implementations use a C preprocessor, so yes, C++ uses the C preprocessor. 大多数(如果不是全部)C ++实现都使用C预处理器,所以是的,C ++使用C预处理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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