Why does the following code generate std::bad_cast
exception?
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::basic_string<char32_t> reg = U"^\\w";
try
{
std::basic_regex<char32_t> tagRegex(reg);
}
catch(std::exception &e)
{
std::cout << e.what() << std::endl;
}
return 0;
}
This sample on Ideone for convenience: https://ideone.com/Saea88
Using char
or wchar
instead of char32_t
runs without throwing though (proof: https://ideone.com/OBlXed ).
You can find here: http://en.cppreference.com/w/cpp/regex/regex_traits :
To use std::basic_regex with other character types (for example, char32_t), a user-provided trait class must be used.
so you would have to implement std::regex_traits<char32_t>
and to see why there is no definition for it see here: Why is there no definition for std::regex_traits<char32_t> (and thus no std::basic_regex<char32_t>) provided?
On GCC or Clang, the code compiles fine even with custom regex traits, but fails at runtime with std::bad_cast
. If you've got yourself here, the issue comes from std::use_facet<std::ctype<char32_t>>
throwing the error, because the current locale doesn't support it. You have to specialize std::ctype<char32_t>
and set the global locale via std::locale::global
to a new locale constructed using the old one and the specialized facet.
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.