Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the u8 string literal necessary in C++11

From Wikipedia:

For the purpose of enhancing support for Unicode in C++ compilers, the definition of the type char has been modified to be at least the size necessary to store an eight-bit coding of UTF-8.

I'm wondering what exactly this means for writing portable applications. Is there any difference between writing this

const char[] str = "Test String"; 

or this?

const char[] str = u8"Test String"; 

Is there be any reason not to use the latter for every string literal in your code?

What happens when there are non-ASCII-Characters inside the TestString?

like image 788
Lukas Schmelzeisen Avatar asked Nov 18 '12 21:11

Lukas Schmelzeisen


People also ask

What characters must enclose a string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

Are string literals read-only in C?

A string literal like "banana" or "apple" is read-only. C allows char* to point to a string literal.

Are C strings utf8?

Within an identifier, you would also want to allow characters >= 0x80, which is the range of UTF-8 continuation bytes. Most C string library routines still work with UTF-8, since they only scan for terminating NUL characters.

Are string literals constant in C?

They are not const in C, but are in C++. They aren't generally writable as noted by others, but they don't behave as constants.


Video Answer


1 Answers

The encoding of "Test String" is the implementation-defined system encoding (the narrow, possibly multibyte one).

The encoding of u8"Test String" is always UTF-8.

The examples aren't terribly telling. If you included some Unicode literals (such as \U0010FFFF) into the string, then you would always get those (encoded as UTF-8), but whether they could be expressed in the system-encoded string, and if yes what their value would be, is implementation-defined.

If it helps, imagine you're authoring the source code on an EBCDIC machine. Then the literal "Test String" is always EBCDIC-encoded in the source file itself, but the u8-initialized array contains UTF-8 encoded values, whereas the first array contains EBCDIC-encoded values.

like image 96
Kerrek SB Avatar answered Sep 23 '22 00:09

Kerrek SB