Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Is wstring cross platform?

I am currently developing on Windows, but I would like to make my application cross platform later on. It does not have/need a GUI.

I am using wstrings all the time, hoping that would be the best solution. I am using the "multibyte charset" in the project settings.

Is wstring supported on all other platforms as well?

like image 668
tmighty Avatar asked Apr 13 '13 10:04

tmighty


2 Answers

Even if std::wstring is supported by other platforms, its definitions and meanings seem different from platform to platform: e.g. wchar_t (which is the basic "building block" for a std::wstring) is 2 bytes in size on Windows, while on on Linux it's 4 bytes.

So, you can use std::wstring to store Unicode strings in UTF-16 format on Windows (which is also the Unicode format used by Win32 APIs), but on Linux your std::wstring should be used to store Unicode UTF-32 strings. So, if the same class (std::wstring) has different meanings on different platforms, I would not define that as portable.

Probably, if you want to write portable code, you could consider using std::string and store Unicode UTF-8 text in it (in fact, std::string is based on char, which is 1 byte on both Windows and Linux).

Or, if you can use the new C++11 standard, you may want to consider std::u16string, which is really portable, since it's defined as basic_string<char16_t>, so it can be used to store Unicode UTF-16 text on every platform, without ambiguity.

like image 93
Mr.C64 Avatar answered Nov 12 '22 07:11

Mr.C64


Is wstring supported on all other platforms as well?

It should be. std::wstring is part of the C++ Input/Output Standard Library, so any conforming implementation of the Standard Library must provide it.

Since it is Standard C++, a program using std::wstring is guaranteed to be portable - at least for what concerns the use of std::wstring.

like image 35
Andy Prowl Avatar answered Nov 12 '22 06:11

Andy Prowl