gpt4 book ai didi

c++ - 将包含不可复制/可移动对象的结构添加到 std::map

转载 作者:太空狗 更新时间:2023-10-29 21:14:55 25 4
gpt4 key购买 nike

我有这个 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 容器?

最佳答案

怎么样

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

?

关于c++ - 将包含不可复制/可移动对象的结构添加到 std::map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39312515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com