Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Is this allowed by the Standard?

like image 658
Martin Ba Avatar asked May 27 '11 07:05

Martin Ba


People also ask

What is the use of Wchar_t?

The wchar_t type is an implementation-defined wide character type. In the Microsoft compiler, it represents a 16-bit wide character used to store Unicode encoded as UTF-16LE, the native character type on Windows operating systems.

Is a character literal constant?

A character literal is composed of a constant character. It's represented by the character surrounded by single quotation marks. There are five kinds of character literals: Ordinary character literals of type char , for example 'a'

How do you declare a wide character in C++?

We can see that to make wide character we have to add 'L' before the character literal. But the character value is not displayed in the output using cout. So to use wide char we have to use wcout, and for taking input we have to use wcin. We can make some wide character array, and print them as string.


1 Answers

This is a bug in VC++.

like image 68
Ignacio Vazquez-Abrams Avatar answered Nov 15 '22 17:11

Ignacio Vazquez-Abrams