c++17更正容器中unique_ptr之间的资源移动
c++17 correct moving resource between unique_ptr in containers
如何更正将资源从已存在的unique_ptr移动到容器中创建的另一个unique_pr?我想用来自另一个unique_ptr的资源将某个unique_pr放入容器中。或者可以将一个unique_ptr从一个容器移动到另一个容器。
这里有一些创建代码:
#include <memory>
#include <map>
#include <string>
struct MyClass
{
std::string s;
};
int main(int argc, char* argv[])
{
std::map<std::string, MyClass> container;
std::unique_ptr<MyClass> tmp = std::make_unique<MyClass>();
tmp->s = "test string";
auto result = container.emplace(std::piecewise_construct,
std::make_tuple("test"),
std::make_tuple(std::move(tmp))
);
return 0;
}
您的地图包含MyClass
,但您正试图将std::unique_ptr<MyClass>
移动到其中。
您需要从std::unique_ptr
中获取实例并移动它。
std::make_tuple(std::move(*tmp))
^___ Dereference the unique_ptr
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- int *ptr = &var 和 int *ptr 之间的区别;*PTR = 和变量?
- (ptr + i)->Func() 和 &ptr[i] 之间有什么区别吗?Func()
- 我如何在void*和boost共享ptr之间切换
- PTR 和 &ptr 之间的区别
- %p 和 cout 和 ptr 之间的C++区别