[cfe-dev] libc++: help diagnosing the following std::atomic compile error

Howard Hinnant hhinnant at apple.com
Mon May 20 17:04:53 PDT 2013


Clang or libc++ bug for __atomic_fetch_add using pointer types?

#include <atomic>
#include <iostream>

template <class T>
T
simulated_atomic_fetch_add(T* ptr, ptrdiff_t val, int)
{
    T tmp = *ptr;
    *ptr += val;
    return tmp;
}

int
main()
{
    int* p = nullptr;
    __atomic_fetch_add(&p, 1, __ATOMIC_RELAXED);
    std::cout << (void*)p << '\n';
    p = nullptr;
    simulated_atomic_fetch_add(&p, 1, __ATOMIC_RELAXED);
    std::cout << (void*)p << '\n';
}

0x1
0x4

I.e. __atomic_fetch_add (and __atomic_fetch_sub) assumes the pointer is a pointer to char.

I can fix this in libc++, or should it be fixed in clang?  The "official" spec at:

http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

is sloppy, and doesn't clarify.  I'm happy to do whatever gcc is doing, and I have no idea what that is.

Howard




More information about the cfe-dev mailing list