Consider a tuple t
of n objects with similar interface (like all have the same base class); n is known during compilation. I need to make a sum as following
std::get<0>(t).foo(vec_0) + std::get<1>(t).foo(vec_1) + ... + std::get<n>(t).foo(vec_n)
I know to add elements of a tuple of numbers, like here, and even variable\functions of an objects in a tuple, e.g.
std::get<0>(t).bar()+std::get<1>(t).bar()...
The situation that I still interesting to find a (template) solution for is the following one:
- all
vec_0,...,vec_n
are different size slices of a bigger vectorVec
. - the information about size of each is known from the tuple as well so actually what I need looks as the following
std::get<0>(t).foo(Vec.head(std::get<0>(t).n)) +
std::get<1>(t).foo(Vec.segment(std::get<0>(t).n, std::get<1>(t).n))+
std::get<2>(t).foo(Vec.segment(std::get<0>(t).n + std::get<1>(t).n, std::get<2>(t).n))+
... +
std::get<n>(t).foo(Vec.tail(std::get<n>(t).n))
Here segment
takes (start, size)
and return required vector; the size is known from std::get<?>(t).n
.