Implementation of string literal concatenation in C and C++

后端 未结 5 1967
北海茫月
北海茫月 2021-01-12 03:05

AFAIK, this question applies equally to C and C++

Step 6 of the \"translation phases\" specified in the C standard (5.1.1.

5条回答
  •  猫巷女王i
    2021-01-12 03:42

    There are tricky rules for how string literal concatenation interacts with escape sequences. Suppose you have

    const char x1[] = "a\15" "4";
    const char y1[] = "a\154";
    const char x2[] = "a\r4";
    const char y2[] = "al";
    

    then x1 and x2 must wind up equal according to strcmp, and the same for y1 and y2. (This is what Heath is getting at in quoting the translation steps - escape conversion happens before string constant concatenation.) There's also a requirement that if any of the string constants in a concatenation group has an L or U prefix, you get a wide or Unicode string. Put it all together and it winds up being significantly more convenient to do this work as part of the "compiler" rather than the "preprocessor."

提交回复
热议问题