Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between L"" and u8""

Is there any difference between the followings?

auto s1 = L"你好";
auto s2 = u8"你好";

Are s1 and s2 referring to the same type?
If no, what's the difference and which one is preferred?

like image 820
MBZ Avatar asked Sep 27 '26 17:09

MBZ


2 Answers

They are not the same type.

s2 is a UTF-8 or narrow string literal. The C++11 draft standard section 2.14.5 String literals paragraph 7 says:

A string literal that begins with u8, such as u8"asdf", is a UTF-8 string literal and is initialized with the given characters as encoded in UTF-8.

And paragraph 8 says:

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

s1 is a wide string literal which can support UTF-16 and UTF-32. Section 2.14.5 String literals paragraph 11 says:

A string literal that begins with L, such as L"asdf", is a wide string literal. A wide string literal has type “array of n const wchar_t”, where n is the size of the string as defined below; it has static storage duration and is initialized with the given characters.

See UTF8, UTF16, and UTF32 for a good discussion on the differences and advantages of each.

A quick way to determine types is to use typeid:

std::cout << typeid(s1).name() << std::endl ;
std::cout << typeid(s2).name() << std::endl ;

On my system this is the output:

PKw
PKc

Checking each of these with c++filt -t gives me:

wchar_t const*
char const*
like image 187
Shafik Yaghmour Avatar answered Sep 30 '26 14:09

Shafik Yaghmour


L"" creates a null-terminated string, of type const wchar_t[]. This is valid in C++03. (Note that wchar_t refers to an implementation-dependent "wide-character" type).

u8"" creates a null-terminated UTF-8 string, of type const char[]. This is valid only in C++11.

Which one you choose is strongly dependent on what needs you have. L"" works in C++03, so if you need to work with older code (which may need to be compiled with a C++03 compiler), you'll need to use that. u8"" is easier to work with in many circumstances, particularly when the system in question normally expects char * strings.

like image 45
nneonneo Avatar answered Sep 30 '26 15:09

nneonneo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!