How to use C++ standard smart pointers with Windows HANDLEs?

后端 未结 10 1088
Happy的楠姐
Happy的楠姐 2020-12-17 19:04

I was wondering if there is a way to use unique_ptr with Windows HANDLEs?

I was thinking to replace the std::default_delete with s

10条回答
  •  甜味超标
    2020-12-17 19:50

    The question can be extended for COM IUnknown pointers - can CComPtr be replaced by any of the standard smart pointers?

    Yes. You don't specialize std::default_deleter, you simply replace the deleter type.

    struct COMDeleter {
        template void operator()(T* ptr) {
            ptr->Release();
        }
    };
    unique_ptr ptr; // Works fine
    

    The same principle applies to shared_ptr and indeed, to HANDLE.

提交回复
热议问题