2

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++?

0

2 Answers 2

4

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

[Later: I was mistaken - I said P0019, which added atomic_ref, which has its own fetch_add calls. Thanks to @Nate for the correction. ]

1 Comment

Note that the latest apple clang version (16) is based on llvm 17 en.m.wikipedia.org/wiki/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.