I'm trying to convert std::string
which contains some accented character to std::wstring
as explained in C++ Convert string (or char*) to wstring (or wchar_t*), but my program throws bad conversion exception.
I'm on Windows 10 and using MSVC 2022 v17.4.1, with language set to C++17.
Here is a minimal reproducible program which demonstrate the issue:
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
#pragma warning( disable : 4996 )
int main()
{
std::string s{ "hello ê world" };
try {
std::wstring ws = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(s);
std::wcout << ws << "\n";
}
catch (const std::exception& e) {
std::cout << e.what() << "\n";
}
}
Any help in converting the above std::string
to std::wstring
is highly appreciated.