开发者

Why is an empty wchar_t literal allowed?

开发者 https://www.devze.com 2023-03-08 22:23 出处:网络
Look at the following code: int main(int argc, ch开发者_如何学运维ar* argv[]) { // This works: (Disable Lang Ext = *Yes* (/Za))

Look at the following code:

int main(int argc, ch开发者_如何学运维ar* 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.

Is this allowed by the Standard?


This is a bug in VC++.


It is not allowed per the ISO standard. 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:

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'. It has to be one or more of the c_char characters. 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.


I would argue that the first example is not allowed, per 2.23.2.1 of the C++ standard:

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’.

(Emphasis mine.)

0

精彩评论

暂无评论...
验证码 换一张
取 消