Skip to content

Instantly share code, notes, and snippets.

@max-dark
Created May 29, 2016 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-dark/3cd1b999fcde16cff18e7e6195075bf3 to your computer and use it in GitHub Desktop.
Save max-dark/3cd1b999fcde16cff18e7e6195075bf3 to your computer and use it in GitHub Desktop.
convert std::string to std::wstring
#include <cstdlib>
#include <cwchar>
#include <string>
#include <memory>
std::wstring to_wstring(const char* str) {
std::unique_ptr<wchar_t[]> tmp = nullptr;
size_t sz, len;
len = mbstowcs(nullptr, str, 0);
sz = len + 1;
tmp.reset(new wchar_t[sz]);
mbstowcs(tmp.get(), str, sz);
return std::wstring(tmp.get());
}
std::wstring to_wstring(const std::string& str) {
return to_wstring(str.c_str());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment