5

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?

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • See [This answer](http://stackoverflow.com/questions/402283/stdwstring-vs-stdstring) for a good rationale of the when and why to use it, but Andy's answer is true - it's in the spec, so it should be reasonably portable – Anya Shenanigans Apr 13 '13 at 10:32
  • 5
    It is portable, but there is very little that you can do with it that is both useful and portable. It's really only used for Windows API calls. Portable applications use strings with known encodings, such as UTF-8, UTF-16, or UTF-32. A `wstring` has a different encoding on different platforms, making it worthless. See http://www.utf8everywhere.org – Dietrich Epp Apr 13 '13 at 10:34

2 Answers2

5

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.

Community
  • 1
  • 1
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
1

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.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • And which charset should I use on Windows in the VS2010 IDE? Is widechar charset ok? I would like to make the cpp files cross-platforms as well. I do not use hardcoded texts often, but sometimes I have to. – tmighty Apr 13 '13 at 10:33
  • @tmighty: Internally, Windows uses Unicode charset, so using multi-byte charset will require an internal conversion from multi-byte to Unicode and vice-versa every time you call a Windows API that works with strings or characters. I'd suggest using the Unicode charset. – Andy Prowl Apr 13 '13 at 10:34
  • Thank you so much. One last question, please: I have not tried it yet. Does for example XCode simply accept cpp files that I created in VS2010? – tmighty Apr 13 '13 at 10:35
  • 1
    @tmighty: I do not know XCode, but if those `cpp` files are using only Standard C++ features (both core language and library, no MS extensions, no Windows-specific headers) it should. – Andy Prowl Apr 13 '13 at 10:36
  • Since `std::wstring` is based on `wchar_t`, and `wchar_t` has _different_ sizes on different platforms (e.g. on Windows it's 2 bytes, on Linux it's 4 bytes), [I would _not_ define `std::wstring` as portable](http://stackoverflow.com/a/15987044/1629821). C++11's new `std::u16string` is portable (if the OP wants to store UTF-16 text in it). – Mr.C64 Apr 13 '13 at 10:58