Accumulate, surprisingly enough, accumulates by default:
return accumulate(x.begin(), x.end(), 0); std::accumulate(v.cbegin(), v.cend(), decltype(*v.cbegin()){});
:) :) :) #include<cstdio>
#include<numeric>
#include<vector>
using namespace std;
template <class T, template<class> class Cont>
T sum(Cont<T> x)
{
return accumulate(x.begin(), x.end(), (T)0);
}
int main(int argc, char *argv[])
{
vector<double> ds({1.1, 2.2, 3.3, 4.4});
vector<float> fs({1.1f, 2.2f, 3.3f, 4.4f});
vector<int> is({1, 2, 3, 4});
vector<unsigned long long> ulls({1ul, 2ul, 3ul, 4ul});
printf("doubles: %f\n",sum(ds));
printf("floats: %f\n",sum(fs));
printf("ints: %d\n",sum(is));
printf("ulls: %llu\n",sum(ulls));
return 0;
}For example: outside the C++ ecosystem, most people probably would stare blank faced if they saw `std::rotate` in a codebase but it's basically a meme at this point in the C++ space.
2,4,6) ...
These overloads participate in overload resolution only if
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.
std::is_execution_policy_v? std::decay_t? std::remove_cvref_t?Really, this madness with C++ needs to stop. People who work on newer versions of the language might be very conservative with the language syntax itself, but they clearly decided having carte blanche to adding an infinite amount of stuff to the standard library.
Could someone please force them to stop, somehow? The whole rest of the industry would be thankful.
Why would I want to add by default?
the main operation is reduction which is fairly basic computer science and taught in any non-BS curriculum - https://en.wikipedia.org/wiki/Reduction_operator (or https://en.wikipedia.org/wiki/Fold_(higher-order_function) which is the standard way to introduce this concept ; check in particular the table showcasing all the implementations in various languages). The standard reduction for a set of numbers to a number that is likely going to be the very first example in your textbook is addition.
The actual operation is addition. I don't care if it's the first example, that doesn't mean you should hide it.
Imagine if print did hello world by default.
Is that actually true? I'm not even sure how hypothetically removing ordering requirements would help you extract performance, let alone any compilers that could do anything with that today. Unless the standard library were to auto-parallelize the reduction, but I doubt they'd do that because the overhead of starting threads would be quite costly for anything but the absolute largest ranges since C++ doesn't have a thread pool sitting idly for you (not to mention that the docs for the function don't mention any thread safety requirements for the BinaryOp and Init which would be required for any such optimization).
Edit: I don't know how to dig into the actual implementation of std::reduce on godbolt - it's not inlined. I think the sibling comment has it right though - one can do adds of four at a time with whatever SIMD extensions are available.
(a, b, c, d) = (0, 0, 0, 0);
for(int i = 0; i < n; i += 4) {
(a, b, c, d) += (arr[i], arr[i+1], arr[i+2], arr[i+3]);
}
return a + b + c + d;The commutative property is fundamental to understanding algebraic structures and fast parallel processing. If a binary operation is commutative over a given set then, yes, a proper compiler will parallelize it. No, it will not necessarily use OS threads directly one to one, that’s usually not a consideration even for a naive approach.