bad_cast 错误 boost::locale::normalize

bad_cast error with boost::locale::normalize

我正在尝试使用 boost 将包含以 utf 编码的 txt 文件内容的字符串转换为 C++ 的 unicode 字符串,并在此之后对其进行规范化。不幸的是,我收到 bad_cast 错误。谁能帮忙? 代码:

convert_to_wstring(void *buffer, int length) {
    boost::locale::generator g;
    g.locale_cache_enabled(true);
    std::locale loc = g(boost::locale::util::get_system_locale());
    std::string buffer_char = static_cast<char *>(buffer);
    std::wstring result = boost::locale::conv::to_utf<wchar_t>(buffer_char, loc);
    result = boost::locale::normalize(result);
    result = boost::locale::fold_case(result);
    return result;
}

我努力过。问题是在某些语言环境中规范化字符串之前应该生成它,所以固定代码如下所示:

convert_to_wstring(void *buffer, int length) {
    boost::locale::generator gen;
    gen.locale_cache_enabled(true);
    std::locale loc = gen(boost::locale::util::get_system_locale());
    std::string buffer_char = static_cast<char *>(buffer);
    std::wstring result = boost::locale::conv::to_utf<wchar_t>(buffer_char, loc);
    std::locale locale = gen("UTF-8");
    std::locale::global(locale);
    result = boost::locale::normalize(result);
    result = boost::locale::fold_case(result);
    return result;
}