I want my template to take lambdas with different number of parameters and be able to handle them.
In the code below template process
takes callable object (function) but doesn’t know whether passed lambda takes arguments or not. Can I somehow check this in template and make the respective call?
void process(auto fn) {
[[maybe_unused]] float res = fn(2.0f);
// Is there a chance to make something like:
// float res = takes-no-args<fn>() ? fn() : fn(2.0f);
// do something with res...
}
int main() {
// This is the default usage
process([](float arg) { return arg; });
// I want to be able to make such a call
process([]() { return 1.0f; });
// Possible, but verbose workaround
process([](float) { return 1.0f; });
}
A simple workaround would be to always pass the lambda with the same signature and ignore the provided argument, but this is way too verbose in case of many arguments.
The goal is to keep client code as simple and clean as possible.
Is there a chance to make something like:
float res = takes-no-args<fn>() ? fn() : fn(2.0f);
Yes, takes-no-args<fn>()
exists in the family of std::is_invocable
type traits, so you could check if fn
is invokable without arguments by using constexpr-if like this:
#include <type_traits>
void process(auto fn) {
float res;
if constexpr (std::is_invocable_r_v<float, decltype(fn)>) {
res = fn(); // invokable without arguments
} else {
res = fn(2.0f);
}
// do something with res...
}
For something more complex than a float
, you might not want to default construct it first, so you could for example use an immediately invoked lambda to avoid that:
void process(auto fn) {
float res = [&]{
if constexpr (std::is_invocable_r_v<float, decltype(fn)>) {
return fn();
} else {
return fn(2.0f);
}
}();
// do something with res...
}