I wanted to write a generic sum function like the following one but not in template syntax but in lambda syntax:
template<typename T>
auto Sum(T lastSummand)
{
return lastSummand;
}
template<typename T, typename... Ts>
auto Sum(T firstSummand, Ts... restSummands)
{
return firstSummand + Sum(restSummands...);
}
Because generic lambdas are mapped to templates it should be possible to do something like:
auto sum = [](auto firstSummand, auto... restSummands) { ... };
But I cannot figure out how to do the recursion using lambdas. Searching in this and other locations did not bring forward much.
In C++14 you don't actually need recursion to do that with generic lambdas.
As an example, you can do this:
Return type is given by the
std::common_type_t
of the given pack.The rest of the code contains the common pattern usually used while waiting for fold expressions.
In C++17 it will become:
See it on wandbox.
If you want to verify on the fly that given parameters are all of arithmetic types, you can use the bool trick as it follows: