Basic_regex抛出带有char32_t的bad_cast
basic_regex throws bad_cast with char32_t
为什么下面的代码会产生std::bad_cast
异常?
#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
使用char
或wchar
代替char32_t
运行而不抛出尽管(证明:https://ideone.com/OBlXed)。
您可以在这里找到:http://en.cppreference.com/w/cpp/regex/regex_traits:
要将std::basic_regex用于其他字符类型(例如char32_t),必须使用用户提供的trait类。
所以你必须实现std::regex_traits<char32_t>
在GCC或Clang上,即使使用自定义正则表达式特征,代码也可以很好地编译,但在运行时使用std::bad_cast
时会失败。如果你在这里,问题来自std::use_facet<std::ctype<char32_t>>
抛出错误,因为当前的语言环境不支持它。您必须专门化std::ctype<char32_t>
,并通过std::locale::global
将全局区域设置为使用旧区域和专门化方面构建的新区域。
相关文章:
- 如何理解C++标准N3337中的expr.const.cast子句8
- C++Cast运算符过载
- 错误:"cast"未命名类型void setCastDescription(std::string
- 通过使用 const-cast 的非常量引用来延长临时的寿命
- "(void) cast"与功能有什么区别 "__attributes__"来沉默未使用的参数警告?
- C++:"Expected '(' for function-style cast or type construction"错误
- 为什么选择 g++ 给予者:"error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]"
- Gtk+ g_signal_connect() 和 C++ lambda 会导致"invalid cast"错误
- Shared_ptr cast vs static_cast speed
- 在 iOS 上使用 Aruco 构建 OpenCV 时"Functional-style cast from id to double is not allowed"
- 覆盖 CAST 运算符(我认为它被称为向下转换)
- C++错误,隐 <function-style-cast> 式要求使用模板化类一次调用多个构造函数的多个转换
- 如何修复<function-style-cast>错误:无法从'initializer list'转换为asdending比较<W>(模板函子)
- C++ C++ 中的函数声明,键入 CAST
- static_cast会丢弃错误,但C风格的演员cast有效
- C++ cast char * to unsigned char
- 禁用 Clang 中的"cast from pointer to smaller type uint32_t"错误
- 是否可以使用gcc 3.3版修复与int*cast相关的Sun Solaris OS 5.8分段故障
- C++ const-cast 一个引用
- 错误 C2440: 'type cast':无法从 'bool' 转换为 'CString'