简体   繁体   中英

Compiles but getting 'uncaught exception of type std::bad_cast'

I'm trying to get started with Boost for C++. Here's a small program that compiles with g++ -Wall test.cpp /usr/local/Cellar/boost/1.55.0/lib/libboost_locale-mt.a .

However, when I run it, here's the error I get: 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;
}

What could be the reason here? Thanks!

I'm on Mac OSX Mavericks.

According to docs:

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())

Convert a NUL terminated string str to lower case according to locale loc

Note: throws std::bad_cast if loc does not have converter facet installed

So, this fixes the problem on my machine.

#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;
}

Outputs:

Variété -> VARIÉTÉ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM