19
votes

I was reading through the std::algorithm documentation at cppreference.com and I noticed a C++17 tag on a lot of cool things I haven't used yet. What got my attention most was the new execution policies. What I gathered from reading about them is that I can make any for_each loop I want multi-threaded just by specifying an execution policy.

For example, I have a program which outputs an image with a 2D graphic on it.

int main(){
    std::for_each(
        img.buffer().begin(),
        img.buffer().end(),
        renderer(
            {-0.5, 0.0, 2.666, 2.0, M_PI / 2.0, 0.0},
            img,
            16
        )
    );
    fout << img;
}

If I want to make this program multi-threaded I should be able to do it with one line.

int main(){
    std::for_each(
        std::execution::par_unseq, // c++17 feature
        img.buffer().begin(),
        img.buffer().end(),
        renderer(
            {-0.5, 0.0, 2.666, 2.0, M_PI / 2.0, 0.0},
            img,
            16
        )
    );
    fout << img;
}

However when I first tried this (with g++ -std=c++17) I got an error telling me that ‘std::execution’ has not been declared, so I tried adding #include <execution> but it says execution: No such file or directory. I've also tried #include<experimental/algorithm> instead of #include<algorithm> but I get the same result. How do I use this new feature?

4
Verify that your g++ supports this feature. C++17 isn't even officially done yet, most likely for another good few months. - chris
libstdc++ doesn't support this feature yet, see that the status of P0024R2 is "no": gcc.gnu.org/onlinedocs/libstdc++/manual/… - user784668
@underscore_d the help text for the duplicate close reason reads something close to "this question has already be answered in ...". It's not necessarily about the Q to be the same. You should answer the old question and then close this one as duplicate. - YSC
(this was a non-specific thought; here I agree it's a duplicate) - YSC

4 Answers

14
votes

was not yet finalized. And various compilers have not yet fully implemented it.

-std=c++17 means "give me all of C++17 you have finished", not "be a perfectly valid C++17 compiler".

This feature is not supported by your compiler and/or standard library at this point. Check back in a few weeks/months/years.

There is no generally accepted "please give me C++17 if you fully support it, and otherwise give me an error" flag you can pass to a compiler; partly because it is of little practical use. If the subset of C++17 they provide is sufficient, then you win. And if you need a fully compliant compiler, specific versions of compilers don't know if they have bugs, so you couldn't trust the flag anyhow and would have to test it against compiler versions. And if you already know what versions of the compiler have sufficiently valid C++17, you don't need a flag to tell you.

2
votes

As far as I understand from cppreference this feature is defined in document P0024R2 and it is not yet supported in any compiler.

1
votes

If you're using g++ then you can try the non-standard extension:

https://gcc.gnu.org/onlinedocs/libstdc++/manual/parallel_mode.html

1
votes

For Microsoft compiler: see C++17 Progress in VS 2017 15.5 and 15.6 where you will find:

Status  Std   Paper   Title
Partial C++17 P0024R2 Parallel Algorithms

For GCC, as Fanael wrote in his comment, see Table 1.5. C++ 2017 Implementation Status at https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017 where you will find

Library Feature                             Proposal    Status
The Parallelism TS Should be Standardized   P0024R2     No