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

Jah*_*hid 14 c++ gcc g++ c++11

使用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);}
Run Code Online (Sandbox Code Playgroud)

抛出错误的一段代码:

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

错误if在行中.可能是什么导致了这个?

Jah*_*hid 22

我解决了它的任何方式.我发现这些行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.
Run Code Online (Sandbox Code Playgroud)

即它表示regex_match不再允许toString()使用match_results 临时字符串(在本例中从函数返回).所以这样的事情解决了这个问题:

std::string tmps=toString(index);
if(std::regex_match(tmps,result,re)){index=fabs(index);}
Run Code Online (Sandbox Code Playgroud)

  • 相关缺陷报告见http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2329. (3认同)