首页 文章

为什么boost :: locale抛出std :: bad_cast?

提问于
浏览
6

我试着理解如何使用boost :: locale来比较忽略大小写和变体的字符串 . 我直接尝试了Boost文档中的代码:

http://www.boost.org/doc/libs/1_51_0/libs/locale/doc/html/collation.html

boost::locale::generator gen;
std::locale vLocale = gen("");


std::wstring a=L"Façade", b=L"facade";

// Following will throw bad_cast
bool eq = std::use_facet<boost::locale::collator<wchar_t>>(vLocale).compare(
    boost::locale::collator_base::secondary,
    a,
    b
) == 0;

if(eq) std::cout << "OK" << std::endl;

此代码在运行时将抛出std :: bad_cast异常 . 我在boost :: locale :: generator的构造函数中尝试了很多参数 . 有谁知道我遇到的问题?

我正在使用C 11和g 4.6以及Boost 1.51.0

1 回答

  • 5

    您似乎使用了不正确的区域设置对象 . 您应该首先使用全局区域设置,然后(如果要使用cout)将区域设置灌输到流中 . Somethig是这样的:

    boost::locale::generator gen;
    std::locale loc = gen("");
    std::locale::global(loc);
    

    但是在您的示例中,如果您不使用cout,只需设置全局区域设置,以便您需要使用构面 .

相关问题