c++ - 错误 : use of deleted function bool regex_match with gcc 5. 2.0

标签 c++ c++11 gcc g++

使用 GCC 4.9.2 编译的代码甚至没有任何警告,但在 GCC 5.2.0 中显示以下错误:

error: use of deleted function ‘bool std::regex_match(const std::__cxx11::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>&&, std::__cxx11::match_results<typename std::__cxx11::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>::const_iterator, _Alloc>&, const std::__cxx11::basic_regex<_Ch_type, _Rx_traits>&, std::regex_constants::match_flag_type) [with _Ch_traits = std::char_traits<char>; _Ch_alloc = std::allocator<char>; _Alloc = std::allocator<std::__cxx11::sub_match<__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> > > >; _Ch_type = char; _Rx_traits = std::__cxx11::regex_traits<char>; typename std::__cxx11::basic_string<_Ch_type, _Ch_traits, _Ch_alloc>::const_iterator = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >]’
     if(std::regex_match(toString(index),result,re)){negative_flag = true;}

抛出错误的一段代码:

bool negative_flag=false;
std::regex re("-[^-]*");
std::smatch result;
if(std::regex_match(toString(index),result,re)){negative_flag = true;}

错误在 if 行。这可能是什么原因造成的?

最佳答案

我解决它的任何方式。我在 regex.h 中找到了这些行:

  // _GLIBCXX_RESOLVE_LIB_DEFECTS
  // 2329. regex_match() with match_results should forbid temporary strings
  /// Prevent unsafe attempts to get match_results from a temporary string.

即它说 regex_match 不再允许带有 match_results 的临时字符串(在这种情况下是从 toString() 函数返回的)。所以这样的事情解决了这个问题:

std::string tmps = toString(index);
if(std::regex_match(tmps,result,re)){negative_flag = true;}

关于c++ - 错误 : use of deleted function bool regex_match with gcc 5. 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32164501/

相关文章:

C++ 模板类定义出错

c++ - std::find_end 作为 Big-O 的复杂性

c++ - 错误 : no type named ‘_traits’ in ‘class date::year_month_day’ ?

c++ - std::vector 与 std::array 性能对比

c++ - C++中方括号内的双点是什么意思

c++ - 无效的用户定义转换为右值引用

c++ - Boost 智能指针 : Can I express it in a more concise way?

c++ - 结构的大小如何随不同的数据类型而变化

c++ - 从源代码编译 Qt 5.3 失败

c++ - std::sort 中的二进制表达式错误(缺少 const)无效操作数:为什么指定比较运算符可以解决这个问题?