简体   繁体   English

为什么允许使用空的 wchar_t 文字?

[英]Why is an empty wchar_t literal allowed?

Look at the following code:看下面的代码:

int main(int argc, char* argv[])
{
    // This works: (Disable Lang Ext = *Yes* (/Za))
    wchar_t wc0 = L'\0';
    wchar_t wc_ = L'';
    assert(wc0 == wc_);

    // This doesn't compile (VC++ 2010):
    char c0 = '\0';
    char c_ = ''; // error C2137: empty character constant
    assert(c0 == c_);
    return 0;
}

Why does the compiler allow defining an empty character literal for wide characters?为什么编译器允许为宽字符定义字符文字 This doesn't make sense for wide, just as it doesn't make sense for char where the compiler flags an error.这对wide没有意义,就像编译器标记错误的char没有意义一样。

Is this allowed by the Standard?这是标准允许的吗?

It is not allowed per the ISO standard.根据 ISO 标准是不允许的。 This is a bug in Microsoft's product.这是微软产品中的一个错误。 Even their page describing that particular feature makes no mention of this aberrant (or abhorrent, depending on your viewpoint) behaviour.甚至他们描述该特定功能的页面也没有提及这种异常(或可恶,取决于您的观点)行为。

The definition for a character literal (as taken from 2.14.3 of C++0x but the relevant bit is unchanged from C++03) contains:字符文字的定义(取自 C++0x 的2.14.3 ,但相关位与 C++03 未更改)包含:

character-literal:
    L’ c-char-sequence ’
c-char-sequence:
    c-char
    c-char-sequence c-char
c-char:
    any member of the source character set except
      the single-quote ’, backslash \, or new-line character
    escape-sequence
    universal-character-name
escape-sequence:
    simple-escape-sequence
    octal-escape-sequence
    hexadecimal-escape-sequence
simple-escape-sequence: one of
    \’ \" \? \\ \a \b \f \n \r \t \v
octal-escape-sequence:
    \ octal-digit
    \ octal-digit octal-digit
    \ octal-digit octal-digit octal-digit
hexadecimal-escape-sequence:
    \x hexadecimal-digit
    hexadecimal-escape-sequence hexadecimal-digit

As you can see, there is no way that you can end up with nothing between the ' characters in L'x' .如您所见,您不可能' L'x'中的字符之间一无所有。 It has to be one or more of the c_char characters.它必须是一个或多个c_char字符。 In fact, this is made explicit in the following paragraph (my emphasis):事实上,这在以下段落中已明确说明(我的重点):

A character literal is one or more characters enclosed in single quotes, as in 'x' , optionally preceded by one of the letters u , U , or L , as in u'y' , U'z' , or L'x' , respectively.字符字面量是用单引号括起来的一个或多个字符,如'x' ,前面可选字母uUL之一,如u'y'U'z'L'x' , 分别。

I would argue that the first example is not allowed, per 2.23.2.1 of the C++ standard:我认为第一个示例是不允许的,根据 C++ 标准的 2.23.2.1:

A character literal is one or more characters enclosed in single quotes, as in 'x' , optionally preceded by the letter L , as in L'x' .字符文字是用单引号括起来的一个或多个字符,如'x' ,可选地前面有字母L ,如L'x'

(Emphasis mine.) (强调我的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM