This C++17 snippet works fine in newer versions of G++
std::for_each(std::execution::par_unseq, container.begin(), container.end(), [&](const auto& element) {
// do something with element... or don't. whatever.
});
When you try to port that code to the current Debian (Stable) distribution for instance, which has G++ 8.3.0 (as of 12/2020), you'll find yourself reading the following error message:
fatal error: execution: File not found
#include <execution>
^~~~~~~~~~~
An obvious solution relying on __has_include that involves a macro is:
#if __has_include(<execution>)
#include <execution>
#define PAR_UNSEQ std::execution::par_unseq,
#else
#define PAR_UNSEQ
#endif
std::for_each(PAR_UNSEQ container.begin(), container.end(), [&](const auto& element) {
// do something with element... or don't. whatever.
});
This compiles fine but has 2 major issues in my eyes:
- It is not a parallel for on systems, that don't have the
<execution>header and - that macro is not something I enjoy looking at.
So is there a better way?
Or if not is there at least a macro solution that actually does a parallel for_each?