21

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?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Willy Goat
  • 1,175
  • 2
  • 9
  • 24
  • 3
    Verify that your g++ supports this feature. C++17 isn't even officially done yet, most likely for another good few months. – chris Mar 02 '17 at 23:35
  • 2
    libstdc++ doesn't support this feature yet, see that the status of P0024R2 is "no": https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#iso.2017.specific –  Mar 02 '17 at 23:38
  • @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 Sep 27 '18 at 09:47
  • (this was a non-specific thought; here I agree it's a duplicate) – YSC Sep 27 '18 at 09:52

4 Answers4

15

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.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 4
    Is there any compiler on any bleeding edge version that supports it ? I tried clang on trunk and gcc 7. – Zitrax Mar 26 '17 at 11:28
  • 1
    `-std=c++17` *means "give me all of C++17 you have finished", not "be a perfectly valid C++17 compiler"*. I thought that was what `-std=c++1z` was for? What am I missing? – Not a real meerkat Apr 09 '18 at 19:39
  • 2
    @cassio 1z is for before C++17 is finished; it may fully implement things that change in the standard, or are removed, etc. 17 is when the standard is done, but your support may be incomplete. Or to put it another way, MSVC just finished support for C++11 in their last patch (ignoring bugs) – Yakk - Adam Nevraumont Apr 09 '18 at 19:59
4

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

2

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

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

sashang
  • 11,704
  • 6
  • 44
  • 58
1

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
Alessandro Jacopson
  • 18,047
  • 15
  • 98
  • 153