std :: basic_string 会优于Windows上的std :: wstring吗?

时间:2023-02-11 20:19:13

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

据我所知,Windows #defines TCHAR作为基于构建的应用程序的正确字符类型 - 因此它在UNICODE构建中是wchar_t,否则为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 是否优于std :: wstring,因为第一个理论上与应用程序的字符类型匹配,而第二个总是宽。

So my question is essentially: Would std::basic_string<TCHAR> be preferable to std::wstring on 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::wstring on Windows and forget about it?

所以我的问题基本上是:std :: basic_string 会优于Windows上的std :: wstring吗?并且,使用std :: basic_string 会有任何警告(即意外行为或副作用)吗?或者,我应该在Windows上使用std :: wstring而忘记它吗?

3 个解决方案

#1


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,年轻人! - )

#2


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 std namespace is supposed to be off limits, but I have not had any problems with the above method for several years.

注意:std命名空间应该是不受限制的,但是几年来我对上述方法没有任何问题。

#3


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。

#1


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,年轻人! - )

#2


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 std namespace is supposed to be off limits, but I have not had any problems with the above method for several years.

注意:std命名空间应该是不受限制的,但是几年来我对上述方法没有任何问题。

#3


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。