Re: Q: Convert std::string to std::wstring using std::ctype widen()
Jeffrey Walton wrote:
Whoops... I broke std::string find (narrow was a substring for which I
was searching in s). DO NOT use narrow.length() + 1.
// Courtesy of Tom Widmer (VC++ MVP)
#include <locale>
std::wstring Widen( std::string const& narrow ) {
typedef std::ctype<wchar_t> CT;
std::wstring wide;
wide.resize( narrow.length() );
Those two lines should compress to (I missed the L before and had the
args in the wrong order):
std::wstring wide(narrow.length(), L'\0');
CT const& ct = std::_USE(std::locale(), CT);
ct.widen( narrow.begin(), narrow.end(), wide.begin() );
For portability, that should be:
ct.widen(&narrow[0], &narrow[0] + narrow.size(), &wide[0]);
The reason is that string iterators are not normally pointers (they are
in VC6, but not in any later VC). You should be careful never to treat
iterators as pointers, or you'll be making yourself major headaches if
you ever need to get the codebase working with a more recent version of VC.
So, to reiterate:
Assuming that std::(w)string is contiguous is a non-standard but
portable assumption (I don't know of a library where it isn't true).
Assuming that std::string::iterator is char* is a non-standard,
non-portable assumption (it doesn't hold reliably for any up-to-date
compiler).
Tom