15

My current understanding of the difference between std::string and std::wstring is simply the buffer's type; namely, char vs wchar_t, respectively.

I've also read that most (if not all) linux distros use char for any and all strings, both ASCII as well as UTF, where Windows is the primary OS that uses wchar_t anymore.

However, there are a few more string types that I want to get straight in my head: u16string and u32string, which are strings with 2-byte and 4-byte buffers, respectively.

So, my question is this:

On platforms with sizeof(wchar_t) == 2, is std::wstring functionally equivalent to std::u16string, as well as platforms with sizeof(wchar_t) == 4 and std::u32string?

5
  • there is another similar question on SO about string/wstring [here <stackoverflow.com/questions/402283/stdwstring-vs-stdstring> Commented Jan 21, 2013 at 12:06
  • 1
    @FloreaMarian Not really - that question is asking the difference between string and wstring. I'm asking whether or not wstring is the same as u16string or u32string depending on the size of wchar_t. Commented Jan 21, 2013 at 12:08
  • 1
    see utf8everywhere.org Commented Jan 22, 2013 at 12:31
  • 2
    @PavelRadzivilovsky Just wanted to thank you almost 2 years later for that link. It completely changed how I design some of my software. Commented Dec 3, 2014 at 16:26
  • 1
    I am happy! Let's spread the better way. utf8everywhere.org FTW :) Commented Dec 4, 2014 at 15:04

1 Answer 1

22

The difference is that the details of char and wchar_t are implementation defined, while the encoding of char16_t and char32_t are explicitly defined by the C++11 standard.

This means that wstring is likely to store the same data as either u16string or u32string, but we don't know which one. And it is allowed for some odd implementation to make them all different, as the size and encoding of the old char types are just not defined by the standard.

Sign up to request clarification or add additional context in comments.

2 Comments

wstring will never be the same as those other string types. The standard requires wchar_t to be a distinct type from char16_t and char32_t. They may in fact have the same underlying type and the same encoding, but is_same<wstring, u16string> will never be true for any valid C++ implementation.
"The same" here means "functionally equivalent", like it says in the question. If sizeof(wchar_t) == 4 you will very likely get the same result from using either a wstring or a u32string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.