chsvlib
chsv helper source code

◆ accumulate()

constexpr auto Chusov::Math::accumulate ( Tpl &&  tpl,
BinOp &&  binop 
) -> decltype(Implementation::accumulate(std::declval<Tpl&&>(), std::declval<BinOp&&>(), std::integral_constant<std::size_t, std::tuple_size<typename std::remove_reference<Tpl>::type>::value>()))
constexprnoexcept

Consequently forwards elements of a tuple to a specified binary functor and returns the result.

Template Parameters
Tplis a type of the tuple containing elements to accumulate.
BinOpis a type of a binary functor specifying the accumulation routine.
Parameters
tplthe tuple.
binopthe binary functor. The functor must have the form Ret binop(T1 arg1, T2 arg2) in which the first element of the tpl tuple and the type Ret must be implicitly convertible to T1, and all other elements of the tuple must be convertible to T2.
Returns
The accumulated value obtained from applying the binop to the first pair of tuple elements, then to the result and subsequent element, and so on.

The function has the same noexcept specifier as a functor applied consequently to the elements of the tuple.

Example:

struct ComplexData
{
int n;
double e;
};
struct Functor
{
constexpr double operator()(double x, double y) const noexcept {return x + y;}
constexpr double operator()(double x, int y) const noexcept {return x + y;}
template <class ComplexType>
constexpr auto operator()(double accum, ComplexType&& data) const noexcept {return accum + data.n + data.e;}
};
static const auto eVal = Chusov::Math::accumulate(std::make_tuple(1, 2, 3.14, ComplexData({5, 6.1}), 7), Functor());
int main(int, char**)
{
std::cout << eVal << "\n"; //Output: 24.24
return 0;
}
constexpr auto accumulate(Tpl &&tpl, BinOp &&binop) noexcept(noexcept(Implementation::accumulate(std::declval< Tpl && >(), std::declval< BinOp && >(), std::integral_constant< std::size_t, std::tuple_size< typename std::remove_reference< Tpl >::type >::value >()))) -> decltype(Implementation::accumulate(std::declval< Tpl && >(), std::declval< BinOp && >(), std::integral_constant< std::size_t, std::tuple_size< typename std::remove_reference< Tpl >::type >::value >()))
Consequently forwards elements of a tuple to a specified binary functor and returns the result.
Definition: chsvmath.h:541