samedi 11 mars 2017

Where to put std::wstring_convert

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.

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>>.

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, e.g.

.h:

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

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

.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?

Or is it better to create an instance within each function implementation where I need to convert between encodings?

Aucun commentaire:

Enregistrer un commentaire