I try to understand how to use boost::locale to compare strings ignoring case and variants. I directly tried a code from Boost documentation :
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;
This code will throw a std::bad_cast exception when running. I tried a lot of parameters in the constructor of boost::locale::generator. Does anyone know about the problem I encounter ?
I am using C++11 with g++4.6 and Boost 1.51.0
It seems you are using an incorrect locale object. First, you should use global locale and then (if you want to use std::cout
) imbue the locale to the stream. Something like this:
boost::locale::generator gen;
std::locale loc = gen("");
std::locale::global(loc);
But, in your example, if you are not using std::cout
, just set global locale so you have the required facets ready.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With