screwnut screwnut - 2 months ago 7
C++ Question

Why is `mem_fn` failing to compile when used with `smatch`?

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

f(m)
. 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);


http://ideone.com/nsN8A1

Answer

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

http://ideone.com/nfuTFX

Comments