我有一个最小可复现的例子:
#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
既不可复制也不可移动。
我的问题:
如何将包含不可复制/移动对象的类添加到std::map
容器中?
const char*
作为映射的键。这是因为它将是指针作为键,而不是指向的内容。 - Some programmer dudeMCVE
。在我的真实项目中,它是std::string
,而且foo
要复杂得多。我想让它变得非常小。谢谢 - Peter VARGA