Jaber
Jaber

Reputation: 17

unresolved external symbol "std::atomic_fetch_add"

Consider this simple code:

#include <iostream>
#include <atomic>

void add(std::atomic<double> & a, double c)
{
    std::atomic_fetch_add(&a, c);
}

int main()
{
    std::atomic<double> a;

    a.store(0);
    std::cout << a.load() << std::endl;

    add(a, 5.0);
    std::cout << a.load() << std::endl;

    std::cin.get();
}

Compiling it will result in:

error LNK2019: unresolved external symbol "double __cdecl std::atomic_fetch_add(struct std::atomic *,double)" (??$atomic_fetch_add@N@std@@YANPAU?$atomic@N@0@N@Z) referenced in function "void __cdecl add(struct std::atomic &,double)" (?add@@YAXAAU?$atomic@N@std@@N@Z)

According to this, atomic_fetch_add is defined in <atomic>, so what is happening?

Upvotes: 0

Views: 682

Answers (1)

Slava
Slava

Reputation: 44268

As stated in documentation:

The standard library provides specializations of the std::atomic template for the following types:

and double is not in the list. There is also note for non member functions:

There are non-member function template equivalents for all member functions of std::atomic. Those non-member functions may be additionally overloaded for types that are not specializations of std::atomic, but are able to guarantee atomicity. The only such type in the standard library is std::shared_ptr.

So double is not supported.

Upvotes: 1

Related Questions