这是MCVE:
main.cpp:14:9: error: call to deleted function 'regex_search'
if (std::regex_search(s(), smatch, regex)) {
^~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:5998:1: note:
candidate function [with _ST = std::__1::char_traits<char>, _SA = std::__1::allocator<char>,
_Ap = std::__1::allocator<std::__1::sub_match<std::__1::__wrap_iter<const char *> > >, _Cp =
char, _Tp = std::__1::regex_traits<char>] has been explicitly deleted
regex_search(const basic_string<_Cp, _ST, _SA>&& __s,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2876:5: note:
candidate function [with _ST = std::__1::char_traits<char>, _SA = std::__1::allocator<char>,
_Ap = std::__1::allocator<std::__1::sub_match<std::__1::__wrap_iter<const char *> > >, _Cp =
char, _Tp = std::__1::regex_traits<char>]
regex_search(const basic_string<_Cp, _ST, _SA>& __s,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2851:5: note:
candidate template ignored: deduced conflicting types for parameter '_Bp'
('std::__1::basic_string<char>' vs. 'std::__1::match_results<std::__1::__wrap_iter<const char
*>, std::__1::allocator<std::__1::sub_match<std::__1::__wrap_iter<const char *> > > >')
regex_search(_Bp, _Bp, const basic_regex<_Cp, _Tp>&,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2857:5: note:
candidate template ignored: could not match 'const _Cp *' against 'std::string' (aka
'basic_string<char, char_traits<char>, allocator<char> >')
regex_search(const _Cp*, const _Cp*,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2863:5: note:
candidate template ignored: could not match 'const _Cp *' against 'std::string' (aka
'basic_string<char, char_traits<char>, allocator<char> >')
regex_search(const _Cp*, match_results<const _Cp*, _Ap>&, const basic_regex<_Cp, _Tp>&,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2869:5: note:
candidate template ignored: could not match 'basic_regex' against 'match_results'
regex_search(const basic_string<_Cp, _ST, _SA>& __s,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:5963:1: note:
candidate template ignored: could not match 'const _CharT *' against 'std::string' (aka
'basic_string<char, char_traits<char>, allocator<char> >')
regex_search(const _CharT* __str, const basic_regex<_CharT, _Traits>& __e,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2839:5: note:
candidate function template not viable: requires at least 4 arguments, but 3 were provided
regex_search(_Bp, _Bp, match_results<_Bp, _Ap>&, const basic_regex<_Cp, _Tp>&,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2845:5: note:
candidate function template not viable: requires at least 4 arguments, but 3 were provided
regex_search(const _Cp*, const _Cp*, match_results<const _Cp*, _Ap>&,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/regex:2884:5: note:
candidate function template not viable: requires at least 4 arguments, but 3 were provided
regex_search(__wrap_iter<_Iter> __first,
^
1 error generated.
编译好:
$ clang ++ -std = c ++ 11 main.cpp
但不是:
$ clang ++ -std = c ++ 14 main.cpp
后一种情况下的错误消息(使用-std = c ++ 14):
$ clang++ -v
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin15.0.0
Thread model: posix
编译器版本信息:
$username = $this->session->userdata['logged_in']['Username'];
echo $username;
那么,出了什么问题?
答案 0 :(得分:9)
从C ++ 11到C ++ 14发生了变化,std::regex_search
不再允许采用r值
template< class STraits, class SAlloc, class Alloc, class CharT, class Traits > bool regex_search( const std::basic_string<CharT,STraits,SAlloc>&&, std::match_results< typename std::basic_string<CharT,STraits,SAlloc>::const_iterator, Alloc>&, const std::basic_regex<CharT, Traits>&, std::regex_constants::match_flag_type flags = std::regex_constants::match_default ) = delete;
这是作为带有const std::string&
禁止接受临时字符串,否则此函数会将match_results m填充为立即生效的字符串迭代器。
因此,从C ++ 14开始,您无法再将临时文件传递给std::regex_search
要修复代码,我们只需将s()
的返回值存储到main中的变量中,然后使用该变量调用std::regex_search
。
#include <iostream>
#include <regex>
std::string s()
{
return "test";
}
int main()
{
static const std::regex regex(R"(\w)");
std::smatch smatch;
auto search = s();
if (std::regex_search(search, smatch, regex)) {
std::cout << smatch[0] << std::endl;
}
return 0;
}
答案 1 :(得分:7)
这在C ++ 11和C ++ 14之间发生了变化。如果我们转到cppreference section for std::regex_search,我们可以看到自从C ++ 14以来删除了带右值引用的重载:
template< class STraits, class SAlloc,
class Alloc, class CharT, class Traits > bool regex_search( const std::basic_string<CharT,STraits,SAlloc>&&,
std::match_results<
typename std::basic_string<CharT,STraits,SAlloc>::const_iterator,
Alloc
>&,
const std::basic_regex<CharT, Traits>&,
std::regex_constants::match_flag_type flags =
std::regex_constants::match_default ) = delete;
由于LWG issue 2329: regex_match()/regex_search() with match_results should forbid temporary strings表示(强调我的)而改变了它:
请考虑以下代码:
const regex r(R"(meow(\d+)\.txt)"); smatch m; if (regex_match(dir_iter->path().filename().string(), m, r)) { DoSomethingWith(m[1]); }
这偶尔会崩溃。问题是 dir_iter-&gt; path()。filename()。string()返回一个临时字符串, so match_results包含一个被破坏的无效迭代器 临时字符串。
regex_match / regex_search(str,reg)可以接受临时 字符串,因为他们只是返回bool。 然而,过载需要 match_results应该禁止临时字符串。
确实如果我们使用非临时的:
std::string s1 = s() ;
if (std::regex_search(s1, smatch, regex)) {
//...
}
它编译( see it live )并且不再显示未定义的行为。
有趣的是,gcc / libstdc ++在C ++ 11模式下也删除了这个重载see it live。由于这是未定义的行为,它似乎是一个很好的解决方案。
此问题也会出现在图书馆的其他区域,请参阅Visual Studio regex_iterator Bug?,该问题涉及同一问题但regex_iterator/regex_token_iterator
。
答案 2 :(得分:2)
这不是一个错误,而是预期的行为。
原因是s()
返回一个临时字符串,regex_search
使用regex_match
,因此如果使用临时字符串,匹配结果将包含不再存在的字符串的迭代器。这可能是未定义的行为。因此,委员会废除了C ++ 14中的regex_search
重载。
您还可以在标准 28.4标题提要[re.syn] 中确认:
template <class ST, class SA, class Allocator, class charT, class traits>
bool regex_search(const basic_string<charT, ST, SA>&&,
match_results<
typename basic_string<charT, ST, SA>::const_iterator,
Allocator>&,
const basic_regex<charT, traits>&,
regex_constants::match_flag_type =
regex_constants::match_default) = delete;
正如您所看到的那样,将basic_string
的右值转移标记为已删除。