[英]Easy UNICODE in C++
Is there a way to make all character sequences UNICODE by default? 有没有一种方法可以使所有字符序列默认为UNICODE? For instance, now I have to say: 例如,现在我不得不说:
std::wstring wstr(L"rofl");
instead, I'd like to say 相反,我想说
std::wstring wstr("rofl");
Thanks! 谢谢!
Visual C++ 8.0 Visual C ++ 8.0
不,您必须使用L""
来指定宽字符串文字。
No, there is no such thing. 不,没有这样的事情。 Many platforms intended for compiliation with either Unicode or ASCII (but not both) provide a macros to allow easy selection of the desired character type (often _
is used, eg _("text")
). 许多旨在与Unicode或ASCII(但不能同时提供)兼容的平台都提供了一个宏,可以轻松选择所需的字符类型(通常使用_
,例如_("text")
)。 You can use this if your issue is compiling for both options. 如果您的问题同时针对这两个选项进行编译,则可以使用此选项。 Otherwise, it's only an additional character. 否则,它只是一个附加字符。
Also, bear in mind the exact encoding of wchar_t
may vary from platform to platform. 另外,请记住,每个平台上wchar_t
的确切编码可能有所不同。 Sometimes it is UTF-16, sometimes it is UTF-32. 有时是UTF-16,有时是UTF-32。 C++1x will add explicit encoding-specified strings u8"string"
u"string"
and U"string"
. C ++ 1x将添加显式编码指定的字符串u8"string"
u"string"
和U"string"
。 (I could be wrong about the prefixes, but there will be such strings). (关于前缀我可能是错的,但是会有这样的字符串)。
我对此感到惊讶:wstring没有wstring(const char *)构造函数吗?
As an aside, CString supports cross-width construction (eg: CString("text") will work for Unicode builds, and CString(L"text") for MBCS builds), but still does a conversion in both cases. 顺便说一句,CString支持跨宽度构造(例如:CString(“ text”)适用于Unicode构建,而CString(L“ text”)适用于MBCS构建),但在两种情况下仍进行转换。 That might accomplish what you want, at the cost of performance. 这可能会以性能为代价实现您想要的功能。
I'd suggest getting used to using _T("text"), as that's generally what you want. 我建议您习惯使用_T(“ text”),因为这通常是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.