This SO question sparked a discussion about std::generate and the guarantees made by the standard. In particular, can you use function objects with internal sta
Anywhere the standard doesn't specify an ordering on an algorithm, you should assume that an implementation can exploit that for parallelism. The paper n3408 discusses options for parallelisation, and points to the Thrust library, which is both a usable parallel-enabled reimplementation of the standard algorithms and a proof-of-concept for future standardisation of parallelism in the algorithms.
Looking at Thrust's implementation of generate, it calls gen in a parallel loop whenever the iterator category is random access. As you've observed, this is consistent with the standard, so you should not assume that generate will always be sequential. (For example, a thread-safe std::rand can be efficiently used with generate and does not require sequential invocation.)
The only algorithms that guarantee sequential invocation are those in numeric; if your code depends on sequential invocation, you should use iota in place of generate. Adapting an existing generator:
template struct iota_adapter {
F f;
operator typename std::result_of::type() { return f(); }
void operator++() {}
};
template iota_adapter iota_adapt(F &&f) { return {f}; }
Use as:
#include
#include
int main() {
int v[5], i = 0;
std::iota(std::begin(v), std::end(v), iota_adapt([&i]() { return ++i; }));
for (auto i: v) std::cout << i << '\n';
}