简体   繁体   English

哪里放置std :: wstring_convert <std::codecvt_utf8<wchar_t> &gt;?

[英]Where to put std::wstring_convert<std::codecvt_utf8<wchar_t>>?

I am planning a new C++11 Win32/64 project with C++Builder 10.1 (Clang 3.3) and thinking about implementing it in the most portable way when it comes to the core functions, so I'd like to use UTF-8 for the std::string encoding (and also, because it's the default encoding for SQLiteCpp , the SQLite C++ wrapper I intend to use). 我正在计划一个带有C ++ Builder 10.1(Clang 3.3)的新C ++ 11 Win32 / 64项目,并考虑在涉及核心功能时以最可移植的方式实现它,因此我想使用UTF- 8为std::string编码(并且,因为它是SQLiteCpp的默认编码,我打算使用SQLite C ++包装器)。

For interacting with the Win-API I decided to use the .to_bytes() and .from_bytes() functions from <codecvt> 's and <locale> 's std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> . 为了与Win-API进行交互,我决定使用<codecvt><locale>std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>.to_bytes().from_bytes()函数。

So, now I'd like to know, what are the best practices where to place the converter object. 因此,现在我想知道将转换器对象放置在哪里的最佳实践是什么。

Should I give it it's own unit and namespace, eg 我应该给它它自己的单元和名称空间,例如

.h: 。H:

...
#include <codecvt>
#include <locale>

namespace cnv
{
    extern std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> wcu8;
}
...

.cpp: .cpp:

...
namespace cnv
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> wcu8;
}
...

and include it everywhere to use cnv::wcu8.to_bytes(xyz) where needed? 并在需要的地方使用cnv::wcu8.to_bytes(xyz)

Or is it better to create an instance within each function implementation where I need to convert between encodings? 还是在需要在编码之间进行转换的每个函数实现中创建一个实例更好?

I wouldn't store the std::wstring_convert in a global variable because that's not thread-safe and doesn't buy you much. 我不会将std::wstring_convert存储在全局变量中,因为它不是线程安全的,并且不会给您带来太多std::wstring_convert There might be a performance hit with instantiating std::wstring_convert everytime you need it, but that should not be your primary concern at the beginning (premature optimization). 每次需要时实例化std::wstring_convert可能会对性能造成影响,但这并不是一开始就应该关注的重点(过早优化)。

So I'd just wrap that thing into functions: 因此,我只是将其包装到函数中:

std::wstring utf8_to_wstr( const std::string& utf8 ) {
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> wcu8;
    return wcu8.from_bytes( utf8 );
}

std::string wstr_to_utf8( const std::wstring& utf16 ) {
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> wcu8;
    return wcu8.to_bytes( utf16 );
}

You have to catch std::range_error exception somewhere. 您必须在某处捕获std::range_error异常。 It can be thrown by std::wstring_convert if the conversion fails for some reason (invalid code points, etc.). 如果由于某种原因(无效的代码点等)而导致转换失败,则可以由std::wstring_convert抛出该错误。

If you hit performance bottlenecks regarding string conversions later, you can still instantiate std::wstring_convert directly at critical points in your code, eg outside of a long running loop that converts many strings. 如果以后遇到有关字符串转换的性能瓶颈,您仍然可以在代码的关键点直接实例化std::wstring_convert ,例如,在长时间运行的循环中,该循环可以转换许多字符串。

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

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