cxxomfort  rel.20210622
Simple backports for C++ - http://ryan.gulix.cl/fossil.cgi/cxxomfort/
<numeric>

Backports related to Standard header <numeric>

// or as part of:

(or automatically by including <cxxomfort/backports.hpp> / <cxxomfort/cxxomfort.hpp>)

Interfaces

Interfaces defined in this section:

Implementation fixes:

Numeric Algorithms with std::move (c++20)

Proposal p0616r0 introduces modifications to the algorithms under <numeric> so that they invoke std::move() on the accumulated cursor. The proposal indicates that this was first intended for C++11 but never proposed because of a lack of "bravery" as it was thought back then that this would introduce a breaking change.

Essentially, the new versions of these algorithms change the underlying operation loop as follows:

// before p0616:
accumulate (ini, fin, v0) ;
{ // loop body
v0 = v0 + *ini;
}
// after p0616:
accumulate (ini, fin, v0) ;
{ // loop body
v0 = std::move(v0) + *ini;
}

Because cxxomfort can not overwrite already existing functions, an opt-in shadowing mechanism is offered instead with the new functions being offered:

using namespace std; // std::accumulate
accumulate (begin(S), end(S), accum, S0); // normal version
// alternative 1: via a different name
using cxxomfort::move_accumulate;
move_accumulate (begin(S), end(S), accum, S0);
// alternative 2: via an extra argument
accumulate (begin(S), end(S), accum, S0, numeric_move_tag() ); // move version

Examples

See Also

File numeric.hpp