Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Here is the 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;
}

It compiles fine with:

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

but not with:

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

Error message in the later case (with -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.

Compiler version information:

$ clang++ -v
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin15.0.0
Thread model: posix

So, what's wrong?

like image 490
Wildcat Avatar asked Oct 15 '15 17:10

Wildcat


2 Answers

There was a change going from C++11 to C++14 where std::regex_search is no longer allowed to take a r-value

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;

This was added as the overload that takes a const std::string&

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

So you can no longer pass a temporary to std::regex_search as of C++14

To fix your code we would simply store the return from s() into a variable in main and use that to call 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

like image 106
NathanOliver Avatar answered Oct 18 '22 10:10

NathanOliver


This changed between C++11 and C++14. If we go to the cppreference section for std::regex_search we can see that overload that takes an rvalue reference was deleted since 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;

It was changed due to LWG issue 2329: regex_match()/regex_search() with match_results should forbid temporary strings which says (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.

and indeed if we use a non-temporary:

std::string s1 = s() ;

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

it compiles (see it live) and no longer exhibits undefined behavior.

Interesting to note that gcc/libstdc++ has this overload deleted in C++11 mode as well see it live. Since this is undefined behavior it seems like a good solution.

This issue also pops up in other areas of the library see Visual Studio regex_iterator Bug? which deals with the same issue but with regex_iterator/regex_token_iterator.

like image 28
Shafik Yaghmour Avatar answered Oct 18 '22 10:10

Shafik Yaghmour