Adding struct containing not copyable/moveable object to std::map

混江龙づ霸主 提交于 2021-02-07 20:13:52

问题


I have this 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 );
}

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

My question:

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


回答1:


What about

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

?



来源:https://stackoverflow.com/questions/39312515/adding-struct-containing-not-copyable-moveable-object-to-stdmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!