【问题标题】:What exactly is the L prefix in C++?C++ 中的 L 前缀究竟是什么?
【发布时间】:2012-10-16 17:40:25
【问题描述】:

我了解它的作用:将字符串文字指定为 const wchar_t *(宽字符串)而不是 const char *(普通旧字符),但它实际上是如何定义的?

它是某种宏吗?它是 GCC 编译器的运算符吗? 是什么

【问题讨论】:

    标签: c++ string widestring


    【解决方案1】:

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

    '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 与 Unicode 没有任何关系。这是关于该主题的extended rant of mine

    【讨论】:

    【解决方案2】:

    称为编码前缀

    2.14.5 字符串文字 [lex.string]

    string-literal:
    | encoding-prefixopt" s-char-sequenceopt"
    | encoding-prefixoptR raw-string
    encoding-prefix:
    | u8
    | u
    | U
    | L

    并标记一个宽字符串文字:

    11) 以L 开头的字符串文字,例如L"asdf",是一个宽 字符串字面量。宽字符串文字的类型为“nconst wchar_t 的数组”,其中 n 是字符串的大小,定义如下;它有 静态存储持续时间,并使用给定的字符进行初始化。

    【讨论】:

      【解决方案3】:

      这里L的意思是宽字符:wchar_t。带L的字符串是用16bit而不是8bit编码的,举个例子:

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

      【讨论】:

        猜你喜欢
        • 2019-02-25
        • 2014-10-26
        • 2012-05-17
        • 2019-08-12
        • 2014-10-28
        • 2012-08-27
        • 2010-11-12
        • 2011-03-18
        • 2011-01-22
        相关资源
        最近更新 更多