Al Bundy Al Bundy - 2 months ago 9
C++ Question

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

I have this MCVE:

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

using foo = 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?

Answer

What about

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

?

Comments