【发布时间】:2023-04-10 20:13:01
【问题描述】:
我有这个 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 );
}
它无法编译,因为std::atomic 既不可复制也不可移动。
我的问题:
如何将包含不可复制/可移动对象的类添加到std::map 容器?
【问题讨论】:
-
你真的不应该使用
const char*作为映射的键。那是因为 pointer 才是关键,而不是它指向的东西。 -
@JoachimPileborg:这是一个
MCVE。在我的实际项目中,它是std::string,而foo要复杂得多。我想让它变得非常小。谢谢 -
为了将来参考,在创建MCVE时,请不要添加其他可能的错误来源,因为那只会让我们感到困惑。实际上创建一个MCVE,尽可能接近你的实际程序。