未解析的外部符号 "std::atomic_fetch_add"
unresolved external symbol "std::atomic_fetch_add"
考虑这个简单的代码:
#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();
}
编译它会导致:
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)
根据this,atomic_fetch_add
在<atomic>
中定义,那么发生了什么?
如documentation所述:
The standard library provides specializations of the std::atomic template for the following types:
和 double
不在列表中。非成员函数也有注释:
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.
所以不支持double
。
考虑这个简单的代码:
#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();
}
编译它会导致:
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)
根据this,atomic_fetch_add
在<atomic>
中定义,那么发生了什么?
如documentation所述:
The standard library provides specializations of the std::atomic template for the following types:
和 double
不在列表中。非成员函数也有注释:
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.
所以不支持double
。