How do I get STL std::string to work with unicode on windows?

前端 未结 9 2094
猫巷女王i
猫巷女王i 2021-02-04 13:29

At my company we have a cross platform(Linux & Windows) library that contains our own extension of the STL std::string, this class provides all sort of functionality on top

9条回答
  •  滥情空心
    2021-02-04 14:00

    No, there is no way to make Windows treat "narrow" strings as UTF-8.

    Here is what works best for me in this situation (cross-platform application that has Windows and Linux builds).

    • Use std::string in cross-platform portion of the code. Assume that it always contains UTF-8 strings.
    • In Windows portion of the code, use "wide" versions of Windows API explicitly, i.e. write e.g. CreateFileW instead of CreateFile. This allows to avoid dependency on build system configuration.
    • In the platfrom abstraction layer, convert between UTF-8 and UTF-16 where needed (MultiByteToWideChar/WideCharToMultiByte).

    Other approaches that I tried but don't like much:

    • typedef std::basic_string tstring; then use tstring in the business code. Wrappers/overloads can be made to streamline conversion between std::string and std::tstring, but it still adds a lot of pain.
    • Use std::wstring everywhere. Does not help much since wchar_t is 16 bit on Windows, so you either have to restrict yourself to BMP or go to a lot of complications to make the code dealing with Unicode cross-platform. In the latter case, all benefits over UTF-8 evaporate.
    • Use ATL/WTL/MFC CString in the platfrom-specific portion; use std::string in cross-platfrom portion. This is actually a variant of what I recommend above. CString is in many aspects superior to std::string (in my opinion). But it introduces an additional dependency and thus not always acceptable or convenient.

提交回复
热议问题