将包含不可复制/可移动对象的结构添加到std :: map [英] Adding struct containing not copyable/moveable object to std::map

查看:61
本文介绍了将包含不可复制/可移动对象的结构添加到std :: map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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 既不可复制也不可移动.

It does not compile because std::atomic is neither copyable nor movable.

我的问题:

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

How can I add a class containing a not copyable/moveable object to a std::map container?

推荐答案

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

?

这篇关于将包含不可复制/可移动对象的结构添加到std :: map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆