Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Algorithms

Accumulation

Suppose we have a dynamic variable that contains an array of integers.

dynamic::variable data = { 1, 20, 300 };

We can calculate the sum of this array with the std::accumulate() algorithm.

As std::accumulate() works on homogeneous types, the initial value must be a dynamic::variable. We could wrap zero with dynamic::variable(0), but in the example below we simply start with a empty dynamic variable. The empty dynamic variable behaves as a zero value when used in integer additions, so the resulting dynamic variable will contain an integer type.

#include <numeric>

// Calculate sum
auto sum = std::accumulate(data.begin(), data.end(), dynamic::variable());
assert(sum.is<int>());
assert(sum == 1 + 20 + 300);


PrevUpHomeNext