Jaber Jaber -4 years ago 66
C++ Question

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download