screwnut screwnut - 1 month ago 4x
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);


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);