Search code examples
c++localestdstring

Why std::locale doesn't work with std::u32string?


So I am trying to use std::tolower with custom locale. It works with wstring, but with u32string I receive:

what(): std::bad_cast

This is what I do

auto loc = std::locale("el_GR.UTF-8");
wstring wtest{L"πρόβλημα"};
u32string utest{U"πρόβλημα"};
auto toLower = [&](auto c) { return std::toupper(c, loc); };
std::transform(wtest.begin(), wtest.end(), wtest.begin(), toLower); //ok
std::transform(utest.begin(), utest.end(), utest.begin(), toLower); //bad_cast

Solution

  • Locales are required to support std::ctype facets only for char and wchar_t, i.e. std::string and std::wstring. Any further specializations are optional.

    It seems that the implementation you are using does not provide such a specialization for char32_t, i.e. std::u32string.