如何创建包含std :: atomic的std :: pair?

Ric*_*k D 6 c++ atomic std std-pair stdatomic

我不知道如何创建以下内容:

std::pair<std::atomic<bool>, int>
Run Code Online (Sandbox Code Playgroud)

我总是总是得到

/usr/include/c++/5.5.0/bits/stl_pair.h:139:45:错误:使用已删除的功能'std :: atomic :: atomic(const std :: atomic&)'
:第一(__x),第二(std :: forward <_U2>(__ y)){}

我试过了

std::pair<std::atomic<bool>, int> pair = std::make_pair(true, 1); //doesn't work
std::pair<std::atomic<bool>, int> pair = std::make_pair({true}, 1); //doesn't work
std::pair<std::atomic<bool>, int> pair = std::make_pair(std::atomic<bool>(true), 1); //doesn't work
std::pair<std::atomic<bool>, int> pair = std::make_pair(std::move(std::atomic<bool>(true)), 1); //doesn't work
Run Code Online (Sandbox Code Playgroud)

我知道std :: atomic是不可复制的,那么您应该如何成对创建它?只是不可能吗?

Bri*_*ian 7

你可以做:

std::pair<std::atomic<bool>, int> p(true, 1);
Run Code Online (Sandbox Code Playgroud)

这用于true初始化原子第一成员,而没有任何无关的副本或移动。在C ++ 17中,保证复制省略还允许您编写:

auto p = std::pair<std::atomic<bool>, int>(true, 1);
Run Code Online (Sandbox Code Playgroud)