skip to Main Content

I have a C++20 program that can be successfully built in Ubuntu 20 with GCC 10 or in Visual Studio 2019. And now I need to compile it for macOS 13.7.2 x64 (currently available on GitHub-hosted runner), which possesses AppleClang 15 compiler.

The program uses std::atomic<float> data types, and fetch_add operation for them as in the example:

#include <atomic>

int main() {
    std::atomic<float> a{0};
    a.fetch_add(1);
}

Unfortunately, it fails to compile in AppleClang 15 with the

error: no member named ‘fetch_add’ in ‘std::atomic’

Online demo: https://gcc.godbolt.org/z/8WvGrczq7

I would like to keep std::atomic<float> data types in the program (to minimize the changes), but replace fetch_add with something else available (probably less performant). Is there such workaround for old Clangs with libc++?

2

Answers


  1. The libc++ status page for C++20 shows that P0019 (the paper that added std::fetch_add) was implemented for clang 19.

    Login or Signup to reply.
  2. Is there such workaround for old Clangs?

    As commented, std::compare_exchange_weak is available for your clang version.

    #include <atomic>
    
    float fetch_add(std::atomic<float>& a, float val) {
        float expected = a;
        while( !a.compare_exchange_weak(expected, expected + val)) {}
        return expected;
    }
    
    int main() {
        std::atomic<float> a{0};
        fetch_add(a,1);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search