在 Windows 上 std::basic_string<TCHAR> 会比 std::wstring 更可取吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/956504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:35:31  来源:igfitidea点击:

Would std::basic_string<TCHAR> be preferable to std::wstring on Windows?

c++windowsunicodestl

提问by Matt Ryan

As I understand it, Windows #defines TCHAR as the correct character type for your application based on the build - so it is wchar_tin UNICODE builds and charotherwise.

据我了解,Windows #define TCHAR 作为基于构建的应用程序的正确字符类型 - 所以它wchar_t在 UNICODE 构建中char

Because of this I wondered if std::basic_string<TCHAR>would be preferable to std::wstring, since the first would theoretically match the character type of the application, whereas the second would always be wide.

因此,我想知道是否比std::basic_string<TCHAR>更可取std::wstring,因为第一个理论上会匹配应用程序的字符类型,而第二个总是宽的。

So my question is essentially: Would std::basic_string<TCHAR>be preferable to std::wstringon Windows? And, would there be any caveats (i.e. unexpected behavior or side effects) to using std::basic_string<TCHAR>? Or, should I just use std::wstringon Windows and forget about it?

所以我的问题本质上是:在 Windows 上会std::basic_string<TCHAR>更可取std::wstring吗?并且,使用 是否有任何警告(即意外行为或副作用)std::basic_string<TCHAR>?或者,我应该只std::wstring在 Windows 上使用而忘记它吗?

回答by Alex Martelli

I believe the time when it was advisable to release non-unicode versions of your application (to support Win95, or to save a KB or two) is long past: nowadays the underlying Windows system you'll support are going to be unicode-based (so using char-based system interfaces will actually complicate the code by interposing a shim layer from the library) and it's doubtful whether you'd save any space at all. Go std::wstring, young man!-)

我相信发布应用程序的非 unicode 版本(以支持 Win95,或节省一两个 KB)的时代已经过去很久了:如今,您将支持的底层 Windows 系统将基于 unicode (因此使用基于字符的系统接口实际上会通过插入库中的填充层使代码复杂化)并且您是否会节省任何空间值得怀疑。去吧std::wstring,年轻人!-)

回答by Brian R. Bondy

I have done this on very large projects and it works great:

我在非常大的项目上做过这个,效果很好:

namespace std
{
#ifdef _UNICODE
    typedef wstring tstring;
#else
    typedef string tstring;
#endif
}

You can use wstring everywhere instead though if you'd like, if you do not need to ever compile using a multi-byte character string. I don't think you need to ever support multi byte character strings though in any modern application.

如果您愿意,您可以在任何地方使用 wstring,如果您不需要使用多字节字符串进行编译。我认为您不需要在任何现代应用程序中支持多字节字符串。

Note: The stdnamespace is supposed to be off limits, but I have not had any problems with the above method for several years.

注意:std命名空间应该是不受限制的,但是我用上面的方法已经好几年没有任何问题了。

回答by ralphtheninja

One thing to keep in mind. If you decide to use std::wstring all the way in your program, you might still need to use std::string if you are communicating with other systems using UTF8.

要记住的一件事。如果您决定在您的程序中一直使用 std::wstring,并且您使用 UTF8 与其他系统进行通信时,您可能仍然需要使用 std::string。