简体   繁体   English

C ++ UNICODE和STL

[英]C++ UNICODE and STL

The Windows API seems big on UNICODE , you make a new project in Visual C++ and it sets it to UNICODE by default. Windows API在UNICODE上看起来很大,你在Visual C ++中创建一个新项目,它默认将它设置为UNICODE
And me trying to be a good Windows programmer, I want to use UNICODE . 我试图成为一名优秀的Windows程序员,我想使用UNICODE

The problem is the C++ Standard Library and STL (such as std::string , or std::runtime_error ) don't work well with UNICODE strings. 问题是C ++标准库和STL(例如std :: stringstd :: runtime_error )与UNICODE字符串不兼容。 I can only pass a std::string , or a char* to std::runtime_error , and i'm pretty sure std::string doesn't support UNICODE . 我只能将std :: stringchar*传递给std :: runtime_error ,我很确定std :: string不支持UNICODE

So my question is, how should I use things such as std::runtime_error ? 所以我的问题是,我应该如何使用诸如std :: runtime_error之类的东西? Should I mix UNICODE and regular ANSI ? 我应该混合使用UNICODE和常规ANSI吗? (I think this is a bad idea...) (我认为这是个坏主意......)
Just use ANSI in my whole project? 在我的整个项目中使用ANSI (prefer not..) Or what? (不喜欢..)或者什么?

In general you shouldn't mix those two encodings. 一般来说,你不应该混合这两种编码。 However, exception messages are something that is only of interest to the developer (eg in log files) and should never be shown to the user (but look at Jim's comment for an important caveat). 但是,异常消息只是开发人员感兴趣的东西(例如在日志文件中),并且永远不应该向用户显示(但请注意Jim的评论中的一个重要警告)。

So you are on the safe side if you use UNICODE for your whole user-faced interface and still use std::exception etc. behind the scenes for developer messages. 因此,如果您将UNICODE用于整个面向用户的界面,并且仍然在开发人员消息的幕后使用std::exception等,那么您就安全了。 There should be no need ever to convert between the two. 应该没有必要在两者之间进行转换。

Furthermore, it's a good trick to define a typedef for UNICODE -independent strings in C++: 此外,在C ++中为UNICODE独立字符串定义typedef是一个很好的技巧:

typedef std::basic_string<TCHAR> tstring;

… and analogously define tcout , tcin etc. conditionally: ......并tcout定义tcouttcin等:

#ifdef UNICODE
    std::wostream& tcout = std::wcout;
    std::wostream& tcerr = std::wcerr;
    std::wostream& tclog = std::wclog;
    std::wistream& tcin = std::wcin;
#else
    std::ostream& tcout = std::cout;
    std::ostream& tcerr = std::cerr;
    std::ostream& tclog = std::clog;
    std::istream& tcin = std::cin;
#endif

Josh, 乔希,

Please have a look at my answer here: https://softwareengineering.stackexchange.com/questions/102205/should-utf-16-be-considered-harmful 请在这里查看我的答案: https//softwareengineering.stackexchange.com/questions/102205/should-utf-16-be-considered-harmful

There is growing number of engineers who believe std::string is just perfect for unicode on Windows, and is the right way to write portable and unicode-correct programs faster. 越来越多的工程师认为std :: string非常适合Windows上的unicode,并且是更快地编写便携式和unicode正确程序的正确方法。

Take a look at this (rather old now) article on CodeProject: Upgrading an STL-based application to use Unicode . 看看CodeProject上的这篇(现在很旧)文章: 升级基于STL的应用程序以使用Unicode It covers the issues you're likely to hit if you're using the STL extensively. 它涵盖了如果您广泛使用STL可能会遇到的问题。 It shouldn't be that bad and, generally speaking, it's worth your while to use wide strings. 它应该不是那么糟糕,一般来说,使用宽字符串是值得的。

要使用Windows Unicode API,只需使用宽字符串版本 - wstring等。它对exception::what()没有帮助,但为此你可以使用UTF-8编码,如果你真的需要Unicode。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM