C 中的L前缀到底是什么

What exactly is the L prefix in C++?

本文关键字:是什么 前缀 中的      更新时间:2023-10-16

我明白它的作用:将字符串字母指定为 const wchar_t *(宽字符串)而不是 const char *(纯旧字符),但是如何实际定义?

它是某种宏吗?是海湾合作委员会编译器的运营商吗?什么是 it?

字面前缀是核心语言的一部分,就像后缀:

'a'    // type: char
L'a'   // type: wchar_t
"a"    // type: char[2]
L"a"   // type: wchar_t[2]
U"a"   // type: char32_t[2]
1      // type: int
1U     // type: unsigned int
0.5    // type: double
0.5f   // type: float
0.5L   // type: long double

请注意, wchar_t具有 nothing 与unicode一起做。这是我关于该主题的延长咆哮。

它称为编码前缀

2.14.5字符串文字[lex.string]

string-literal
|encoding-prefix opt " s-char-sequenceopt"
|encoding-prefix opt r raw-string
encoding-prefix
|u8
|u
|U
|L

标记一个宽字的字面文字:

11)以 L(例如 L"asdf")开头的字符串文字是一个宽 字符串字面量。一个宽字的字面字面具有" n const wchar_t的数组"类型,其中N是字符串的大小,如下所示;它有 静态存储持续时间,并用给定的字符初始化。

l这里的含义是宽字符: wchar_t。带有L的字符串以16位而不是8位编码,以一个示例:

"A"    = 41
L"A"   = 00 41