user3496846 user3496846 - 3 months ago 11
C++ Question

basic_regex throws bad_cast with char32_t

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

Answer

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?

Comments