c++templatesfunctional-programmingbarrier

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:


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).