简单的 std::regex_search() 代码无法使用 Apple clang++ -std=c++14 进行编译

Simple std::regex_search() code won't compile with Apple clang++ -std=c++14

这是 MCVE:

#include <iostream>
#include <regex>

std::string s()
{
    return "test";
}

int main()
{
    static const std::regex regex(R"(\w)");
    std::smatch smatch;

    if (std::regex_search(s(), smatch, regex)) {
        std::cout << smatch[0] << std::endl;
    }

    return 0;
}

编译良好:

$ clang++ -std=c++11 main.cpp

但不包括:

$ clang++ -std=c++14 main.cpp

后一种情况下的错误消息(使用 -std=c++14):

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++ -v
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin15.0.0
Thread model: posix

那么,怎么了?

从 C++11 到 C++14 发生了变化,其中 std::regex_search 不再允许采用右值

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&

的重载添加的

is prohibited from accepting temporary strings, otherwise this function populates match_results m with string iterators that become invalid immediately.

因此从 C++14

开始,您不能再将临时传递给 std::regex_search

为了修复您的代码,我们只需将 s() 中的 return 存储到 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;
}

Live Example

这在 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 上面写着 (emphasis mine):

Consider the following code:

const regex r(R"(meow(\d+)\.txt)");
smatch m;
if (regex_match(dir_iter->path().filename().string(), m, r)) {
  DoSomethingWith(m[1]);
}

This occasionally crashes. The problem is that dir_iter->path().filename().string() returns a temporary string, so the match_results contains invalidated iterators into a destroyed temporary string.

It's fine for regex_match/regex_search(str, reg) to accept temporary strings, because they just return bool. However, the overloads taking match_results should forbid temporary strings.

事实上,如果我们使用非临时的:

std::string s1 = s() ;

if (std::regex_search(s1, smatch, regex)) {
//...
}

它编译(see it live)并且不再表现出未定义的行为。

有趣的是,gcc/libstdc++ 也在 C++11 模式中删除了这个重载 see it live。由于这是未定义的行为,这似乎是一个很好的解决方案。

这个问题也出现在库的其他区域,请参阅 ,它处理相同的问题,但 regex_iterator/regex_token_iterator

这不是错误,而是预期的行为。 原因是 s() returns 是一个临时字符串,regex_search 使用了 regex_match 因此,如果使用临时字符串,匹配结果将包含指向不再存在的字符串的迭代器存在。这将是未定义的行为。因此,委员会在 C++14 中废除了这个 regex_search 重载。

你也可以在标准28.4 Header synopsis中确认[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 的重载已标记为已删除。