std::make_unique 可以将函数的输出作为参数吗?

Can std::make_unique take the output of a function as an argument?

有没有办法使用 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 loadedSurface3 = std::make_unique<SDL_Surface, SurfaceDeleter>(IMG_Load(path.c_str())));

SurfaceDeleter 是一个函子。

loadedSurface1 和 loadedSurface2 都工作正常。 loadedSurface3 失败(没有重载函数的实例匹配参数列表)

如果无法使 loadedSurface3 正常工作,是否推荐 loadedSurface2 而不是 loadedSurface1 因为它使用 make_unique?

Is there a way to use make_unique and pass the output of a function as a parameter?

std::make_unique<T> 采用构造函数参数来创建新的 T。它不需要 T* 到现有的 T.

此外,you can't specify a deleter with it like that.

你的意思很简单:

std::unique_ptr<SDL_Surface, SurfaceDeleter> loadedSurface3{IMG_Load(path.c_str())};

is loadedSurface2 recommended over loadedSurface1 because it uses make_unique?

我看不到任何好处。你所做的只是将你的构造分成两行并丢失删除器。