I am trying to compute the sum of specific elements of a tuple, but cannot get it to compile.
Here is what the function I want to write would look like:
template <int ...ids>
float sum(std::tuple<int, int, std::string, float> things)
{
return "<sum of elements of ids>";
}
And I would like to call it, like this:
std::tuple<int, int, std::string, float> my_things= { 1, 2, "3", 4.0f };
float sum_numbers = sum<0, 1, 3>(my_things);
I couldn't get it to work using folding. :/ Is this even possible? If yes, how so?
Thanks in advance!
I couldn't get it to work using folding. :/ Is this even possible? If yes, how so?
What about as follows ?
template <int ...ids>
float sum (std::tuple<int, int, std::string, float> things)
{
return ( 0.0f + ... + std::get<ids>(things) );
}
But I suggest (1) to be more generic and (2) to use std::size_t
for indexes
template <std::size_t ... Ids, template T>
auto sum (T const & things)
{ return ( 0 + ... + std::get<Ids>(things) ); }