Here is the MCVE:
#include
#include
std::string s()
{
return \"test\";
}
int main()
{
static const std::regex regex(
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.