Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is basic_string conversion via iterators legal?

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.

like image 223
EOG Avatar asked Dec 16 '25 11:12

EOG


1 Answers

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

like image 167
Brian Bi Avatar answered Dec 18 '25 23:12

Brian Bi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!