C++ 将boost::iequals与std::u16string一起使用

C++ 将boost::iequals与std::u16string一起使用,c++,boost,locale,C++,Boost,Locale,我正在尝试使用boost对两个std::u16string实例进行不区分大小写的字符串比较。根据我的搜索,我需要生成一个区域设置,我正在这样做 #include <boost/algorithm/string.hpp> #include <boost/locale.hpp> #include <locale> #include <iostream> int main() { // Create the strings std::

我正在尝试使用boost对两个std::u16string实例进行不区分大小写的字符串比较。根据我的搜索,我需要生成一个区域设置,我正在这样做

#include <boost/algorithm/string.hpp>
#include <boost/locale.hpp>

#include <locale>
#include <iostream>

int main() {
    // Create the strings
    std::u16string str1 = boost::locale::conv::utf_to_utf<char16_t>("unicode");
    std::u16string str2 = boost::locale::conv::utf_to_utf<char16_t>("UNICODE");

    // Create the locale
    boost::locale::generator gen;
    std::locale loc = gen("");

    // Doesn't matter if I do this or not
    //std::locale::global(loc);

    // Try to compare
    if (boost::iequals(str1, str2, loc)) {
        std::cout << "EQUAL\n";
    } else {
        std::cout << "!EQUAL\n";
    }

    return 0;
}

我做错了什么?

std::u16string
使用
char16\t
(如您所知)

boost::iequals
在内部使用
std::toupper
来比较两个字符串

std::toupper
需要
std::ctype
中的facet支持,在我们的例子中,
ct=char16\t
。正如本文所解释的,该标准不需要这种支持,因此在大多数实现中都缺乏这种支持

facet std::ctype需要专门化并放入已使用的facet中,以支持字符类型的加宽、缩小和分类。char16\u t或char32\u t没有现成的专门化


所以你没有做错什么,只是没有支持。如果您确实需要16位unicode字符串支持,我建议您查看第三方库,例如
Qt
,其中类
QString
使用16位字符。

std::u16string
使用
char16\t
(如您所知)

boost::iequals
在内部使用
std::toupper
来比较两个字符串

std::toupper
需要
std::ctype
中的facet支持,在我们的例子中,
ct=char16\t
。正如本文所解释的,该标准不需要这种支持,因此在大多数实现中都缺乏这种支持

facet std::ctype需要专门化并放入已使用的facet中,以支持字符类型的加宽、缩小和分类。char16\u t或char32\u t没有现成的专门化

所以你没有做错什么,只是没有支持。如果您确实需要16位unicode字符串支持,我建议您查看第三方库,例如
Qt
,其中类
QString
使用16位字符

terminate called after throwing an instance of 'std::bad_cast'
  what():  std::bad_cast