乐闻世界logo
搜索文章和话题

Why is shared_ptr< void > legal, while unique_ptr< void > is ill- formed ?

1个答案

1

In C++, std::shared_ptr<void> is valid, while std::unique_ptr<void> is invalid, primarily due to differences in how they handle type conversion and object destruction.

1. Type Conversion

std::shared_ptr<void> is valid because std::shared_ptr supports implicit type conversion. Smart pointers of type void can point to objects of any type, allowing safe storage of pointers without knowing the specific object type. For example, we can convert a std::shared_ptr<int> to std::shared_ptr<void>:

cpp
std::shared_ptr<int> sp1 = std::make_shared<int>(10); std::shared_ptr<void> sp2 = sp1; // implicit conversion

Here, sp2 can successfully store a pointer to an int-type object, but it loses information about the specific object type.

2. Object Destruction

std::unique_ptr<void> is invalid primarily because std::unique_ptr must be able to correctly destroy the object it points to when its lifetime ends. Since void is an incomplete type, the compiler cannot determine how to properly destroy objects pointed to by void. For example, you cannot do this:

cpp
std::unique_ptr<int> up1 = std::make_unique<int>(10); std::unique_ptr<void> up2 = std::move(up1); // error: cannot destroy void type

If you attempt the above code, the compiler will report an error because std::unique_ptr<void> cannot be implicitly converted from std::unique_ptr<int>, and it does not know how to destroy memory pointed to by void.

Conclusion

In short, std::shared_ptr<void> allows storage of pointers of any type, facilitating pointer passing when the type is unknown, and std::shared_ptr has its own control block to manage object lifetime and reference counting without needing to know the specific object type. Conversely, std::unique_ptr requires complete type information to ensure objects can be correctly destroyed, hence std::unique_ptr<void> is invalid.

This design reflects the differences in usage scenarios and purposes between std::shared_ptr and std::unique_ptr. std::shared_ptr is more suitable for cases requiring type erasure or multiple owners, while std::unique_ptr is used for scenarios requiring explicit ownership and type guarantees.

2024年6月29日 12:07 回复

你的答案