I have the following third-party API:
using StatisticsFunc = double (*)(const std::vector<double> &)
libraryClass::ComputeStatistics(StatisticsFunc sf);
Which I'm using like this:
obj1->ComputeStatistics([](const auto& v) {return histogram("obj1", v);};
obj2->ComputeStatistics([](const auto& v) {return histogram("obj2", v);};
But all those lambdas are just repeated code. I'd rather have it like this:
obj1->ComputeStatistics(getHistogramLambda("obj1"));
So I need to define:
constexpr auto getHistogramLambda(const char* name) {
return [](const auto& v) {return histogram(name, v);};
}
But it won't work, because name
is not captured. Neither will this work:
constexpr auto getHistogramLambda(const char* name) {
return [name](const auto& v) {return histogram(name, v);};
}
Because capturing lambda is not stateless anymore and cannot be cast to function pointer.
Ofc one can do it as a macro, but I want a modern C++ 17 solution.
Passing string as template argument seems an option as well:
https://stackoverflow.com/a/28209546/7432927 , but I'm curious if there's a constexpr
way of doing it.