Why must C/C++ string literal declarations be sing

2019-04-03 19:25发布

Is there any particular reason that multi-line string literals such as the following are not permitted in C++?

string script =
"
      Some
   Formatted
 String Literal
";

I know that multi-line string literals may be created by putting a backslash before each newline. I am writing a programming language (similar to C) and would like to allow the easy creation of multi-line strings (as in the above example).

Is there any technical reason for avoiding this kind of string literal? Otherwise I would have to use a python-like string literal with a triple quote (which I don't want to do):

string script =
"""
      Some
   Formatted
 String Literal
""";

Why must C/C++ string literal declarations be single-line?

10条回答
Lonely孤独者°
2楼-- · 2019-04-03 19:54

You can also do:

string useMultiple =  "this" 
                      "is "
                      "a string in C."; 

Place one literal after another without any special chars.

查看更多
劫难
3楼-- · 2019-04-03 19:57

The terse answer is "because the grammar prohibits multiline string literals." I don't know whether there is a good reason for this other than historical reasons.

There are, of course, ways around this. You can use line splicing:

const char* script = "\
      Some\n\
   Formatted\n\
 String Literal\n\
";

If the \ appears as the last character on the line, the newline will be removed during preprocessing.

Or, you can use string literal concatenation:

const char* script = 
"      Some\n"
"   Formatted\n"
" String Literal\n";

Adjacent string literals are concatenated during preprocessing, so these will end up as a single string literal at compile-time.

Using either technique, the string literal ends up as if it were written:

const char* script = "      Some\n   Formatted\n  String Literal\n";
查看更多
仙女界的扛把子
4楼-- · 2019-04-03 20:01

Actually, you can break it up thus:

string script =
"\n"
"      Some\n"
"   Formatted\n"
" String Literal\n";

Adjacent string literals are concatenated by the compiler.

查看更多
霸刀☆藐视天下
5楼-- · 2019-04-03 20:02

Strings can lay on multiple lines, but each line has to be quoted individually :

string script =
    "                \n"
    "       Some     \n"
    "    Formatted   \n"
    " String Literal ";
查看更多
地球回转人心会变
6楼-- · 2019-04-03 20:03

Others have mentioned some excellent workarounds, I just wanted to address the reason.

The reason is simply that C was created at a time when processing was at a premium and compilers had to be simple and as fast as possible. These days, if C were to be updated (I'm looking at you, C1X), it's quite possible to do exactly what you want. It's unlikely, however. Mostly for historical reasons; such a change could require extensive rewrites of compilers, and so will likely be rejected.

查看更多
\"骚年 ilove
7楼-- · 2019-04-03 20:05

Literal declarations doesn't have to be single-line.

GPUImage inlines multiline shader code. Checkout its SHADER_STRING macro.

查看更多
登录 后发表回答