screwnut screwnut - 1 year ago 78
C++ Question

Why is mem_fn failing to compile when used with smatch::str?

This looks straightforward and innocent enough, yet fails with pages of "failed substitutions" at the call to

. Why is that?

string const input = "The quick brown fox.";
std::regex const words("[^\\s]+");

auto f = std::mem_fn(&std::smatch::str);
std::sregex_iterator i = std::sregex_iterator(input.begin(), input.end(), words);
std::smatch m = *i;

string first_word = f(m);

Answer Source

Because str takes an argument. Normally it defaults to 0, but a mem_fn doesn't bring along default argument values and thus it must be explicitly supplied.

string first_word = f(m, 0);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download