c++ - 为什么不能交换 std::atomic<T> ?

标签 c++ c++11 std standards atomic

#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/

相关文章:

c++ - 在编译时选择全局作用域函数

c++ - c++中std::min(int)的效率

C++ 使用命名空间声明

c++ - 在 C++ 中对这些独立但相关的序列进行排序的简洁方法是什么?

c++ - 仅在需要时编译文件

c++ - 显式解析类成员的范围

c++ - lambda : the function is not captured 的 Lambda

c++ - std::stod 忽略小数点后的非数值

c++ - 明确删除赋值运算符时允许从右值赋值?

c++ - 如何从C++代码中绘制类图