basic_regex throws bad_cast with char32_t

北城余情 提交于 2021-02-11 06:51:07

问题


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


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/39126756/basic-regex-throws-bad-cast-with-char32-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!