2021年1月21日星期四

sophisticated summation of elements in objects residing in a std:tuple

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:

  1. all vec_0,...,vec_n are different size slices of a bigger vector Vec.
  2. 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.

https://stackoverflow.com/questions/65824317/sophisticated-summation-of-elements-in-objects-residing-in-a-stdtuple January 21, 2021 at 05:16PM

没有评论:

发表评论