gpt4 book ai didi

c++ - 为什么 shared_ptr 没有专门化?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:51 26 4
gpt4 key购买 nike

shared_ptr<void>特殊之处在于,根据定义,它将通过调用 delete 来调用未定义的行为在 void* 上.

那么,为什么没有 shared_ptr<void>抛出编译错误的特化?

最佳答案

Using shared_ptr to hold an arbitrary object

shared_ptr can act as a generic object pointer similar to void*. When a shared_ptr instance constructed as:

shared_ptr<void> pv(new X);

is destroyed, it will correctly dispose of the X object by executing ~X.

This propery can be used in much the same manner as a raw void* is used to temporarily strip type information from an object pointer. A shared_ptr can later be cast back to the correct type by using static_pointer_cast.

但是如何呢?

This constructor has been changed to a template in order to remember the actual pointer type passed. The destructor will call delete with the same pointer, complete with its original type, even when T does not have a virtual destructor, or is void

关于c++ - 为什么 shared_ptr<void> 没有专门化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7881003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com