consider the following piece of code:
#include <iostream>
#include <string>
#include <codecvt>
std::wstring string_to_wstring(const std::string& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
return converter.from_bytes(str);
}
int main()
{
std::string str = "abcä"; // without the "ä" it works
std::wstring wstr = string_to_wstring(str);
std::wcout << wstr << L"\n";
}
This throws me a "bad_conversion" exception, which seems to be caused by the umlaut because if I remove the "ä", everything works.
I have found the code for the string_to_wstring function some time ago here on SO and it worked until now very well. Mainly because I never came accross any umlauts.
Can we fix this function to work with any characters? Or is there a better (more efficient/safe) way to convert between string and wstring?