Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

stl

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?

like image 507
Peter VARGA Avatar asked Jun 16 '26 05:06

Peter VARGA


1 Answers

What about

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

?

like image 144
max66 Avatar answered Jun 18 '26 20:06

max66