1. unique_ptr支持模板参数为一个数组类型,而shared_ptr不能这样做; See Section 5.2.1,page 80
std::unique_ptr<int[]> p(new int[10]); // OK
std::shared_ptr<int[]> p(new int[10]); // Error: does not compile
2. unique_ptr支持操作符[],而shared_ptr不支持,这是unique_ptr考虑到性能和可扩展性。 See Section 5.2.1,page 81
std::unique_ptr<int, void(*)(int*)> p(new int[10], [] (int* p) { delete[] p; });
The C++ Standard Library: shared_ptr and unique_ptr
最新推荐文章于 2023-10-11 21:11:10 发布