If I do:
std::unique_ptr<AVFrame, AVFrameDeleter> avFrameUniquePtr(av_frame_alloc());
I get no errors. Observe that
av_frame_alloc()
returns AVFrame*
However if I do
std::unique_ptr<AVFrame, AVFrameDeleter> avFrameUniquePtr = std::make_unique<AVFrame,AVFrameDeleter>(av_frame_alloc());
I get
no instance of overloaded function "std::make_unique" matches the argument list -- argument types are: (AVFrame *)
For me it should be ok. What's happening?
What's happening?
Unfortunately, std::make_unique
can't specify the deleter type. Its 2nd template parameter is used for the type of the list of arguments passed to it. The code fails because you specify it as AVFrameDeleter
explicitly, but pass AVFrame*
, they don't match.
Unlike
std::make_shared
(which hasstd::allocate_shared
),std::make_unique
does not have an allocator-aware counterpart. A hypothetical allocate_unique would be required to invent the deleter type D for theunique_ptr<T,D>
it returns which would contain an allocator object and invoke both destroy and deallocate in its operator().
On the other hand, the following code works (even it's not what you expect).
// the 1st template parameter is specified as AVFrame
// the 2nd template parameter is deduced as {AVFrame*}
// construct a std::unique_ptr<AVFrame, std::default_delete<AVFrame>>
std::make_unique<AVFrame>(av_frame_alloc());
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments