Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is a portable to way to declare a `std::barrier` with no completion function?

I tried the following in https://cpp.sh/. As you can see, I did not find a one-liner example that works across compilers. In particular, I wonder about MSVC static asserts in seemingly standard code:

#include <barrier>
#include <functional>

class Class {
    // missing template argument
    // std::barrier my_barrier1;
    
    // static assert N4861 in MSVC
    // std::barrier<void(*)(void) noexcept> my_barrier2;
    
    // static assert N4861 in MSVC
    // std::barrier<std::function<void()>> my_barrier3;
    
    // std::function does not like noexcept
    // std::barrier<std::function<void() noexcept>> my_barrier3b;
    
    // compiles in MSVC but not here
    // std::barrier<std::_No_completion_function> my_barrier4;
    
    // copied from MSVC - works in both
    struct _No_completion_function {
        void operator()() noexcept {}
    };
    std::barrier<_No_completion_function> my_barrier5;
};

int main() {
    return 0;
}

Am I overlooking an obvious way of declaring a std::barrier with no completion function?

Related regarding N4861, but ultimately not helpful:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

std::barrier<> my_barrier;

In some contexts std::barrier my_barrier; will work too.

std::barrier<> and std::barrier are not the same thing. The latter formally requires class template argument deduction, which isn’t allowed in some contexts (e.g. for non-static data members, which is what you’re seeing).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading