gpt4 book ai didi

c++ - basic_regex 使用 char32_t 抛出 bad_cast

转载 作者:行者123 更新时间:2023-11-30 02:29:59 31 4
gpt4 key购买 nike

为什么下面的代码会产生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

使用 charwchar 而不是 char32_t 运行时不会抛出(证明:https://ideone.com/OBlXed)。

最佳答案

您可以在这里找到: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.

所以你必须实现std::regex_traits<char32_t>

要了解为什么没有定义,请看这里:Why is there no definition for std::regex_traits<char32_t> (and thus no std::basic_regex<char32_t>) provided?

关于c++ - basic_regex 使用 char32_t 抛出 bad_cast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39126756/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com