C++ 中的可移植 wchar_t
C++ 中有可移植的 wchar_t 吗? 在 Windows 上,它是 2 个字节。 其他都是 4 个字节。 我想在我的应用程序中使用 wstring,但是如果我决定移植它,这会导致问题。
Is there a portable wchar_t in C++? On Windows, its 2 bytes. On everything else is 4 bytes. I would like to use wstring in my application, but this will cause problems if I decide down the line to port it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正在处理程序内部的使用,请不要担心; A 类中的 wchar_t 与 B 类中的相同。
如果您打算在 Windows 和 Linux/MacOSX 版本之间传输数据,那么您需要担心的不仅仅是 wchar_t,并且您需要想出处理方法所有细节。
您可以定义一个类型,在任何地方都将其定义为四个字节,并实现您自己的字符串等(因为 C++ 中的大多数文本处理都是模板化的),但我不知道这是否能满足您的需求。
类似于 typedef int my_char; typedef std::basic_string; my_string;
If you're dealing with use internal to the program, don't worry about it; a wchar_t in class A is the same as in class B.
If you're planning to transfer data between Windows and Linux/MacOSX versions, you've got more than wchar_t to worry about, and you need to come up with means to handle all the details.
You could define a type that you'll define to be four bytes everywhere, and implement your own strings, etc. (since most text handling in C++ is templated), but I don't know how well that would work for your needs.
Something like
typedef int my_char; typedef std::basic_string<my_char> my_string;
“便携式 wchar_t”是什么意思? 有一种
uint16_t
类型,它到处都是 16 位宽,这种类型经常可用。 但这当然还没有构成一个字符串。 字符串必须知道其编码才能理解length()
、substring()
等函数(因此它不会在字符串中间剪切字符)使用 utf8 或 16 时的代码点)。 我知道您可以使用一些与 unicode 兼容的字符串类。 所有这些都可以免费在商业程序中使用(Qt 将在几个月内免费兼容商业程序,当 Qt 4.5 发布时)。ustring
来自gtkmm
项目。 如果您使用 gtkmm 或使用 glibmm 进行编程,那应该是首选,它内部使用utf-8
。Qt
还有一个字符串类,称为 QString。 它采用utf-16
编码。ICU
是另一个创建可移植 unicode 字符串类的项目,并且有一个UnicodeString
类,内部似乎是用 utf-16 编码的,就像 Qt 一样。 不过还没用过那个。What do you mean by "portable wchar_t"? There is a
uint16_t
type that is 16bits wide everywhere, which is often available. But that of course doesn't make up a string yet. A string has to know of its encoding to make sense of functions likelength()
,substring()
and so on (so it doesn't cut characters in the middle of a code point when using utf8 or 16). There are some unicode compatible string classes i know of that you can use. All can be used in commercial programs for free (the Qt one will be compatible with commercial programs for free in a couple of months, when Qt 4.5 is released).ustring
from thegtkmm
project. If you program with gtkmm or use glibmm, that should be the first choice, it usesutf-8
internally.Qt
also has a string class, called QString. It's encoded inutf-16
.ICU
is another project that creates portable unicode string classes, and has aUnicodeString
class that internally seems to be encoded in utf-16, like Qt. Haven't used that one though.拟议的 C++0x 标准将具有
char16_t
和char32_t
类型。 在那之前,您将不得不重新使用整数来表示非wchar_t
字符类型。根据标准,您需要专门化
char_traits
为整数类型。 但在 Visual Studio 2005 上,我已经摆脱了std::basic_string
的困扰,没有进行任何特殊处理。那么您需要使用 UTF-16,而不是
wchar_t
。SQLite API 也有 UTF-8 版本。 您可能想使用它而不是处理
wchar_t
差异。The proposed C++0x standard will have
char16_t
andchar32_t
types. Until then, you'll have to fall back on using integers for the non-wchar_t
character type.According to the standard, you'll need to specialize
char_traits
for the integer types. But on Visual Studio 2005, I've gotten away withstd::basic_string<CHAR32>
with no special handling.Then you'll need to use UTF-16, not
wchar_t
.The SQLite API also has a UTF-8 version. You may want to use that instead of dealing with the
wchar_t
differences.我的建议。 使用 UTF-8 和 std::string。 宽弦不会给你带来太多的附加值。 因为无论如何你都不能将宽字符解释为字母,因为某些字符是从多个 unicode 代码点创建的。
因此,在任何地方都可以使用 UTF-8 并使用好的库来处理自然语言。 例如 Boost.Locale。
坏主意:定义类似 typedef
uint32_t mychar;
的东西是不好的。 由于您不能将 iostream 与它一起使用,因此您无法创建基于此字符的字符串流,因为您将无法在其中写入。例如,这不起作用:
不会为您创建字符串。
My suggestion. Use UTF-8 and std::string. Wide strings would not bring you too much added value. As you anyway can't interpret wide character as letter as some characters crated from several unicode code points.
So use anywhere UTF-8 and use good library to deal with natural languages. Like for example Boost.Locale.
Bad idea: define something like typedef
uint32_t mychar;
is bad. As you can't use iostream with it, you can't create for example stringstream based in this character as you would not be able to write in it.For example this would not work:
Would not create you a string.