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

后端 未结 3 666
-上瘾入骨i
-上瘾入骨i 2020-12-07 01:23

Here is the MCVE:

#include 
#include 

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

int main()
{
    static const std::regex regex(         


        
3条回答
  •  时光取名叫无心
    2020-12-07 01:58

    This not a bug, but expected behaviour. The reason is that s() returns a temporary string, regex_search makes use of regex_match and consequently if a temporary string was utilized match results would contain iterators to a string that no longer exists. This would have been undefined behaviour. Thus, the committee abolished this regex_search overload in C++14.

    You can also confirm in the standard 28.4 Header synopsis [re.syn]:

    template 
    bool regex_search(const basic_string&&,
    match_results<
    typename basic_string::const_iterator,
    Allocator>&,
    const basic_regex&,
    regex_constants::match_flag_type =
    regex_constants::match_default) = delete;
    

    As you can see the overload that takes a rvalue to a basic_string is marked deleted.

提交回复
热议问题