dimanche 4 juin 2017

Constructing the string in the regex cause "no matching function for call to regex_search"

Following code is very usual and work like a charm.

#include <regex>
#include <string>

using namespace std;

int main ()
{
    string inputString("123454");
    char expression[] = "123";

    string str_expression(expression);
    regex test(str_expression);

    smatch sm_match;
    regex_search(inputString, sm_match, test);
    return 0;
}

However, when I merge the two lines:

string str_expression(expression);
regex test(str_expression);

into

regex test(string(expression));

The compiler failed and replied that:

error: no matching function for call to 
‘regex_search(std::__cxx11::string&, std::__cxx11::smatch&,
std::__cxx11::regex (&)(std::__cxx11::string))’

This is quite strange, the compiler is complaining that test is not a legal template in arguments of regex_search.

Thanks in advance for your answering.

Aucun commentaire:

Enregistrer un commentaire