gpt4 book ai didi

C++:wstring 是跨平台的吗?

转载 作者:太空狗 更新时间:2023-10-29 21:02:13 25 4
gpt4 key购买 nike

我目前正在 Windows 上开发,但我想稍后让我的应用程序跨平台。它没有/不需要 GUI。

我一直在使用 wstrings,希望这是最好的解决方案。我在项目设置中使用“多字节字符集”。

所有其他平台也支持 wstring 吗?

最佳答案

即使std::wstring受其他平台支持,其定义和含义似乎因平台而异:例如wchar_t (这是 std::wstring 的基本“构建 block ”) is 2 bytes in size on Windows, while on on Linux it's 4 bytes .

因此,您可以使用 std::wstring在 Windows 上以 UTF-16 格式存储 Unicode 字符串(这也是 Win32 API 使用的 Unicode 格式),但在 Linux 上您的 std::wstring应该用于存储 Unicode UTF-32 字符串。因此,如果同一个类 ( std::wstring) 在不同平台上有不同的含义,我不会将其定义为可移植的。

如果您想编写可移植代码,您可以考虑使用 std::string并在其中存储 Unicode UTF-8 文本(事实上,std::string 是基于 char 的,它在 Windows 和 Linux 上都是 1 个字节)。

或者,如果您可以使用新的 C++11 标准,您可能需要考虑 std::u16string ,它非常便携,因为它被定义为 basic_string<char16_t> , 因此它可以用于在每个平台上存储 Unicode UTF-16 文本,而不会产生歧义。

关于C++:wstring 是跨平台的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15986810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com