我正在尝试使用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_castAbort 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;
}
这可能是什么原因?谢谢!
我在Mac OSX Mavericks上。
最佳答案
根据文档:
std::basic_string<CharType> boost::locale::to_lower(CharType const *str,
std::locale const &loc = std::locale())
根据语言环境loc将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;
}
输出:
Variété -> VARIÉTÉ