首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

string literal

句法

" (unescaped_character|escaped_character)* "

(1)

?

L " (unescaped_character|escaped_character)* "

(2)

?

u8 " (unescaped_character|escaped_character)* "

(3)

(since C++11)

u " (unescaped_character|escaped_character)* "

(4)

(since C++11)

U " (unescaped_character|escaped_character)* "

(5)

(since C++11)

prefix(optional) R "delimiter( raw_characters )delimiter"

(6)

(since C++11)

解释

unescaped_character

-

Any valid character except the double-quote ", backslash \, or new-line character

escaped_character

-

See escape sequences

prefix

-

One of L, u8, u, U

delimiter

-

A character sequence made of any source character but parentheses, backslash and spaces (can be empty, and at most 16 characters long)

raw_characters

-

Any character sequence, except that it must not contain the closing sequence )delimiter"

1%29窄多字节字符串文字。无前缀字符串的类型为const char[]...

2%29宽字符串文字。a的类型L"..."字符串文字是const wchar_t[]...

3%29 UTF-8编码字符串文字.。a的类型u8"..."字符串文字是const char[]...

4%29 UTF-16编码字符串文字.。a的类型u"..."字符串文字是const char16_t[]...

5%29 UTF-32编码字符串文字.。a的类型U"..."字符串文字是const char32_t[]...

6%29生字串文字。用于避免转义任何字符,分隔符之间的任何内容都成为字符串的一部分。前缀,如果存在的话,具有与上面描述的相同的含义。

注记

空字符%28'\0',,,L'\0',,,char16_t(),etc%29总是追加到字符串文本:因此,字符串文本"Hello"const char[6]保持字符'H',,,'e',,,'l',,,'l',,,'o',和'\0'...

并排放置的字符串文本被连接在翻译阶段6%28后的预处理%29。也就是说,"Hello," " world!"生成%28单%29字符串"Hello, world!"如果两个字符串的编码前缀%28相同,或者两个字符串都没有%29,则产生的字符串将具有相同的编码前缀%28或没有前缀%29。

If one of the strings has an encoding prefix and the other doesn't, the one that doesn't will be considered to have the same encoding prefix as the other. L"Δx = %" PRId16 // at phase 4, PRId16 expands to "d" // at phase 6, L"Δx = %" and "d" form L"Δx = %d" If a UTF-8 string literal and a wide string literal are side by side, the program is ill-formed.

(since C++11)

任何编码前缀的其他组合都可能被实现所支持,也可能不被支持。这种级联的结果是实现定义的。

字符串文字静态存储持续时间,从而存在于程序生命的记忆中。

字符串文本可用于初始化字符数组如果数组初始化为char str[] = "foo";,,,str将包含字符串的副本。"foo"...

允许编译器,但不需要若要组合相等或重叠字符串文本的存储空间,请执行以下操作。这意味着,通过指针进行比较时,相同的字符串文本可能比较相等,也可能不相同。

二次

代码语言:javascript
复制
bool b = "bar" == 3+"foobar" // could be true or false, implementation-defined

二次

尝试修改字符串文本的结果。未定义行为*它们可以存储在只读存储器%28中,例如.rodata%29或与其他字符串文本组合:

二次

代码语言:javascript
复制
const char* pc = "Hello";
char* p = const_cast<char*>(pc);
p[0] = 'M'; // undefined behavior

二次

在C中,字符串文字是类型的。char[],并可直接分配给%28 non-const%29。char*.C++03也允许它使用%28,但反对它,因为文字是const在C++%29中。C++11不再允许在没有强制转换的情况下进行这种分配。

字符串文本不一定是C字符串:如果字符串文本嵌入了空字符,则表示包含多个字符串的数组。

二次

代码语言:javascript
复制
const char* p = "abc\0def"; // std::strlen(p) == 3, but the array has size 8

二次

如果一个有效的十六进制数字跟随字符串文本中的十六进制转义,它将无法编译为无效的转义序列。字符串连接可用作解决方法:

二次

代码语言:javascript
复制
//const char* p = "\xfff"; // error: hex escape sequence out of range
const char* p = "\xff""f"; // OK: the literal is const char[3] holding {'\xff','f','\0'}

二次

窄多字节字符串文本%281%29和宽字符串文本%282%29的编码是实现定义的。例如,gcc使用命令行选项-fexec-charset-fwide-exec-charset...

二次

代码语言:javascript
复制
#include <iostream>
 
char array1[] = "Foo" "bar";
// same as
char array2[] = { 'F', 'o', 'o', 'b', 'a', 'r', '\0' };
 
const char* s1 = R"foo(
Hello
World
)foo";
//same as
const char* s2 = "\nHello\nWorld\n";
 
int main()
{
    std::cout << array1 << '\n';
    std::cout << array2 << '\n';
 
    std::cout << s1;
    std::cout << s2;
}

二次

产出:

二次

代码语言:javascript
复制
Foobar
Foobar
 
Hello
World
 
Hello
World

二次

另见

字符串文字的C文档

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com