以下代码无法链接:
#include <atomic>
struct A
{
unsigned long a;
unsigned long b;
};
struct B
{
void set(A tmp)
{
_a.store(tmp);
}
std::atomic<A> _a;
};
int main()
{
B b;
b.set(A());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
出现以下错误:
/tmp/cc8gyaZM.o: In function `std::atomic<A>::store(A, std::memory_order)':
dryn.cpp: (.text._ZNSt6atomicI1AE5storeES0_St12memory_order[_ZNSt6atomicI1AE5storeES0_St12memory_order]+0x3e): undefined reference to `__atomic_store_16'
Run Code Online (Sandbox Code Playgroud)
如果我用大小为int的任何东西替换unsigned long-s,那么编译就好了.使用g ++ 4.7.2.你知道为什么会这样吗?
用命令编译:
g++ dryn.cpp --std=c++11
Run Code Online (Sandbox Code Playgroud) 我遇到这个编译错误
function std::atomic::is_lock_free() const: error: undefined reference to '__atomic_is_lock_free'
在Linux上使用gcc 4.7.2编译代码如下所示.
struct S {
int a;
int b;
};
std::atomic<S> s;
cout << s.is_lock_free() << endl;
Run Code Online (Sandbox Code Playgroud) 众所周知:https://stackoverflow.com/a/32694707/1558037
页面1104:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
C++11§29.5/ 1说
有一个泛型类模板atomic.模板参数T的类型应该是可以轻易复制的(3.9).
§3.9告诉
标量类型,平凡可复制类类型(第9节),此类类型的数组以及这些类型的cv限定版本(3.9.3)统称为平凡可复制类型.
全文:
1 There is a generic class template atomic<T>. The type of the template argument T shall be trivially copyable (3.9). [ Note: Type arguments that are not also statically initializable may be difficult to use.
— end note ]
2 The semantics of the operations on specializations of atomic are defined in 29.6.
3 Specializations and instantiations of the atomic template shall have a deleted copy …Run Code Online (Sandbox Code Playgroud)