basic_regex使用char32_t抛出bad_cast [英] basic_regex throws bad_cast with char32_t

查看:68
本文介绍了basic_regex使用char32_t抛出bad_cast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码为什么会生成 std :: bad_cast 异常?

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;
}

为方便起见,此示例在Ideone上: https://ideone.com/Saea88

This sample on Ideone for convenience: https://ideone.com/Saea88

使用 char wchar 而不是 char32_t 运行但不会抛出错误(证明:

Using char or wchar instead of char32_t runs without throwing though (proof: https://ideone.com/OBlXed).

推荐答案

您可以在此处找到:

要将std :: basic_regex与其他字符类型(例如char32_t)一起使用,必须使用用户提供的特征类.

To use std::basic_regex with other character types (for example, char32_t), a user-provided trait class must be used.

所以您必须实现 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?

这篇关于basic_regex使用char32_t抛出bad_cast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆