Current solution
You can use std::wstring_convert
to convert a string
to or from wstring
, using a codecvt
to specify the conversion to be performed.
Example of use:
string so=u8"Jérôme Ângle";
wstring st;
wstring_convert<std::codecvt_utf8<wchar_t>,wchar_t> converter;
st = converter.from_bytes(so);
If you have a c-string (array of char), the overloads of from_bytes()
will do exactly what you want:
char p[]=u8"Jérôme Ângle";
wstring ws = converter.from_bytes(p);
Online demo
Is it sustainable ?
As pointed out in the comments, C++17 has deprecated codecvt
and the wstring_convert
utility:
These features are hard to use correctly, and there
are doubts whether they are even specified correctly. Users should use
dedicated text-processing libraries instead.
In addition, a wstring
is based on wchar_t
which has a very different encoding on linux systems and on windows systems.
So the first question would be to ask why a wstring
is needed at all, and why not just keep utf-8 everywhere.
Depending on the reasons, you may consider to use: