Why allow shared_ptr?

前端 未结 2 1034
渐次进展
渐次进展 2021-02-18 15:44

This answer cites N4082, which shows that the upcoming changes to std::shared_ptr will allow both T[] and T[N] variants:

2条回答
  •  青春惊慌失措
    2021-02-18 16:07

    Unless I'm mistaken, a Y(*)[N] could only be formed by taking the address of an array, which clearly can't be owned or deleted by a shared_ptr.

    Don't forget that shared_ptr is a generic resource management utility and can be constructed with a custom deallocator:

    template shared_ptr(Y* p, D d);
    

    Such a user-provided deallocator can perform an action other than delete/delete[]. For example if the the array under question is an array of file descriptors, the "deallocator" can just close all of them.

    In such cases the shared_ptr doesn't own the object in the widely used sense and therefore can be bound to an existing array by taking its address.

提交回复
热议问题