编译但得到“类型为std :: bad_cast的未捕获异常”

con*_*com 2 c++ boost

我正在尝试使用Boost for C ++。这是一个用编译的小程序g++ -Wall test.cpp /usr/local/Cellar/boost/1.55.0/lib/libboost_locale-mt.a

但是,当我运行它时,这是我得到的错误: libc++abi.dylib: terminating with uncaught exception of type std::bad_cast: std::bad_cast Abort trap: 6

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

int main(void) {
    char test[] = "Variété";
    boost::locale::to_upper(test);
    std::cout << test << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这可能是什么原因?谢谢!

我在Mac OSX Mavericks上。

doo*_*des 5

根据文档:

http://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/group__convert.html#ga7889a57e1bc1059fbb107db0781d0b6d

    std::basic_string<CharType> boost::locale::to_lower(CharType const *str,
                                   std::locale const &loc = std::locale())
Run Code Online (Sandbox Code Playgroud)

根据区域设置将NUL终止的字符串str转换为小写

注意: 如果loc没有安装转换器方面,则抛出std :: bad_cast

因此,这解决了我机器上的问题。

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

int main(void) {
    std::string test = "Variété";
    std::locale loc = boost::locale::generator().generate("en_US.UTF-8");
    std::string test_u = boost::locale::to_upper(test, loc);
    std::cout << test << " -> " << test_u << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

Variété -> VARIÉTÉ
Run Code Online (Sandbox Code Playgroud)