Is iterator a propper conversion between basic_strings? Is this safe/legal? Does this create a valid string?
std::u16string str16;
//str16 is set here;
std::string cStr(str16.cbegin(), str16.cend());
In VS 2013 it seem to work ok.
Is iterator a propper conversion between basic_strings? Is this safe/legal? Does this create a valid string?
std::u16string str16;
//str16 is set here;
std::string cStr(str16.cbegin(), str16.cend());
In VS 2013 it seem to work ok.
Yes, it is legal, because a char
can be initialized from a char16_t
. However, the result of the conversion is implementation-defined if the char
is signed and the value is too large to fit (C++11 §4.7/3). (Whether char
is signed or unsigned is also implementation-defined, §3.9.1/1.)
As pointed out in comments, this will not convert your string into another encoding. (That being said, it will probably work in the case that the string contains only ASCII characters. That being said, don't do it.)