为什么mem_fn在与smatch::str一起使用时编译失败?

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

本文关键字:编译 失败 一起 str mem fn 在与 smatch 为什么      更新时间:2023-10-16

这看起来很简单,但在调用f(m)时出现了"失败的替换"页面。为什么呢?

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

因为str接受参数。通常默认为0,但mem_fn没有缺省参数值,因此必须显式提供。

string first_word = f(m, 0);
http://ideone.com/nfuTFX