问题 错误:使用已删除的函数bool regex_match和gcc 5.2.0


使用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)){index=fabs(index);}

抛出错误的一段代码:

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

错误发生在 if 线。可能是什么导致了这个?


8377
2017-08-23 08:04


起源



答案:


我解决了它的任何方式。我找到了这些线 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 不再允许临时字符串(从中返回) toString() 在这种情况下的功能)与match_results。所以这样的事情解决了这个问题:

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

16
2017-08-23 08:17



相关的缺陷报告在 open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2329。 - Alan Stokes


答案:


我解决了它的任何方式。我找到了这些线 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 不再允许临时字符串(从中返回) toString() 在这种情况下的功能)与match_results。所以这样的事情解决了这个问题:

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

16
2017-08-23 08:17



相关的缺陷报告在 open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2329。 - Alan Stokes