C++ std::make_unique能否将函数的输出作为参数?
有没有办法使用make_unique并将函数的输出作为参数传递C++ std::make_unique能否将函数的输出作为参数?,c++,smart-pointers,unique-ptr,C++,Smart Pointers,Unique Ptr,有没有办法使用make_unique并将函数的输出作为参数传递 auto loadedSurface1 = std::unique_ptr<SDL_Surface,SurfaceDeleter>(IMG_Load(path.c_str())); auto loadedSurface2 = std::make_unique<SDL_Surface, SurfaceDeleter>(); loadedSurface2.reset(IMG_Load(path.c_str()))
auto loadedSurface1 = std::unique_ptr<SDL_Surface,SurfaceDeleter>(IMG_Load(path.c_str()));
auto loadedSurface2 = std::make_unique<SDL_Surface, SurfaceDeleter>();
loadedSurface2.reset(IMG_Load(path.c_str()));
auto loadedSurface3 = std::make_unique<SDL_Surface, SurfaceDeleter>(IMG_Load(path.c_str())));
SurfaceDeleter是一个函子
loadedSurface1和loadedSurface2都可以正常工作。loadedSurface3失败重载函数的实例与参数列表不匹配
如果无法使loadedSurface3工作,是否建议loadedSurface2超过loadedSurface1,因为它使用make_unique
有没有办法使用make_unique并将函数的输出作为参数传递
std::make_unique使用构造函数参数来创建新的T。它不使用T*来创建现有的T
此外
你的意思很简单:
std::unique_ptr<SDL_Surface, SurfaceDeleter> loadedSurface3{IMG_Load(path.c_str())};
由于loadedSurface2使用“使_唯一”,因此是否建议将loadedSurface1置于loadedSurface2之上
我看不出有什么好处。你所取得的一切就是把你的建筑分成两行,然后失去删除器
有没有办法使用make_unique并将函数的输出作为参数传递
std::make_unique使用构造函数参数来创建新的T。它不使用T*来创建现有的T
此外
你的意思很简单:
std::unique_ptr<SDL_Surface, SurfaceDeleter> loadedSurface3{IMG_Load(path.c_str())};
由于loadedSurface2使用“使_唯一”,因此是否建议将loadedSurface1置于loadedSurface2之上
我看不出有什么好处。你所取得的成就就是将你的结构分成两行,然后失去删除器。你说它失败是什么意思?到底发生了什么?演示a。它是如何失败的?请详细说明。当您执行std::make_unique时,您并没有指定unique_ptr的删除器的类型,而是指定SurfaceDeleter是std::make_unique的第一个参数的类型。不能使用std::make_unique创建带有自定义删除器的唯一_ptr。对比一下。你为什么在评论中回答:@LightnessRacesinOrbit我们对答案的理解不同。我的评论和你的回答都不能回答这个问题。你说失败是什么意思?到底发生了什么?演示a。它是如何失败的?请详细说明。当您执行std::make_unique时,您并没有指定unique_ptr的删除器的类型,而是指定SurfaceDeleter是std::make_unique的第一个参数的类型。不能使用std::make_unique创建带有自定义删除器的唯一_ptr。对比一下。你为什么在评论中回答:@LightnessRacesinOrbit我们对答案的理解不同。我的评论和你的回答都不能回答这个问题。@user250095正确@用户250095正确;