#include <atomic>
int main()
{
auto a = std::atomic_int(1);
auto b = std::atomic_int(2);
std::swap(a, b); // error
}
错误信息:
error: no matching function for call to 'swap(std::atomic&, std::atomic&)'
为什么不能
std::atomic<T>
被交换?
最佳答案
std::atomic
具有已删除的复制构造函数,并且没有移动构造函数。
因此,它既不是移动可分配的,也不是移动可构造的。
因此 std::swap
不能在任何 std::atomic
上调用类型。
引用:
https://en.cppreference.com/w/cpp/algorithm/swap
关于c++ - 为什么不能交换 std::atomic<T> ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61656491/