如何从std :: regex_error获取解释性字符串?

Cla*_*diu 2 c++ regex error-handling c++11

我的节目正在投掷std::regex_error().我想知道错误是什么,因为正则表达式对我来说是合法的.我基本上做了这个:

try {
    // offending code
} catch (std::regex_error& e) {
    log_error("Regex error: " << e.what() << ", " << e.code());
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Regex error: regex_error, 4
Run Code Online (Sandbox Code Playgroud)

这不是特别有用.4是什么意思?code()en.cppreference.com条目只说:

返回传递给std :: regex_error构造函数的std :: regex_constants :: error_type.

error_type的条目给出了错误代码列表,所有错误代码都是"未指定"的.

我没有办法,只能这样做吗?

switch (e.code()) {
    case std::regex_constants::error_collate: return "error_collate";
    case std::regex_constants::error_ctype: return "error_ctype";
    // etc ...
}
Run Code Online (Sandbox Code Playgroud)

ric*_*ici 8

这是标准C++库中的一个实现质量问题,这是一个很好的方式来说它是一个bug.GCC错误67361,确切地说("std :: regex_error :: what()应该说一些关于error_code的内容").

错误报告中最近提交了一个补丁,所以我想它最终会显示为升级.

与此同时,你几乎没有选择,只能推出自己的代码 - >消息转换功能.(或者,作为临时调试方法,请咨询include/bits/regex_error.h)