问题
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