For function parameters of template functions I find myself often wrapping member functions inside a lambda to create identical standalone functions with the first parameter being the object.
A (dummy) example :
class A
{
public:
double f1() const;
double f2(int i) const;
// some data
};
template <typename Func>
double calculateSum(std::vector<A> as, Func f)
{
double result = 0.0;
for (auto a : as)
result += f(a);
return result;
}
int main()
{
std::vector<A> as;
int i = 0;
auto sum1 = calculateSum(as, [](const A& a) { return a.f1(); });
auto sum2 = calculateSum(as, [&i](const A& a) { return a.f2(i); });
return 0;
}
Is there a way to define such lambdas more generically ? or is there a way to directly refer to the member functions instead of using lambdas ?