c++ - 将包含不可复制/可移动对象的结构添加到 std::map

标签 c++ c++11 stl

我有这个 MCVE:

#include <atomic>
#include <map>
#include <string>

struct foo
{
    int intValue;
    std::atomic_bool bar;

    foo( int intValue ) : intValue( intValue ) {};
};

std::map<std::string, foo> myMap;

int main()
{
    myMap.emplace( "0",  1234 );
}

它无法编译,因为 std::atomic 既不可复制也不可移动。

我的问题:

如何将包含不可复制/可移动对象的类添加到 std::map 容器?

最佳答案

怎么样

myMap.emplace(std::piecewise_construct,
              std::forward_as_tuple("0"),
              std::forward_as_tuple(1234));

?

关于c++ - 将包含不可复制/可移动对象的结构添加到 std::map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39312515/

相关文章:

依赖于编译器特定代理的 C++11 库功能

c++ - 为什么初始化列表不能作为main的参数?怎么提议呢?

c++ - 未隐式声明移动赋值运算符

c++ - STLPort 和 SGI STL 的区别

c++ - 数独递归,接受零为合法

c++ - 获取 "does not name a type error",但我正在命名一个类型

c++ - 在 C++ 中,有任何处理内存分配/删除的一般准则吗?

c++ - 在抛出 'std::invalid_argument' what() : stoi 实例后终止调用

c++ - 使用指向 STL vector 的指针是个好主意吗?

algorithm - 微软的STL::list::sort()使用的是哪种排序算法?