C++17:unique_ptr<char[]> 和 shared_ptr<char[] 之间的指针存储差异>
C++17: Difference in pointer storage between unique_ptr<char[]> and shared_ptr<char[]>
我感到困惑:
unique_ptr<char[]> u_ptr = make_unique<char[]>(10);
sprint(u_ptr.get(), 10, "milos"); // get returns char*
cout << u_ptr.get(); // get returns char*
和:
shared_ptr<char[]> s_ptr = make_shared<char[]>(10);
sprint(*s_ptr.get(), 10, "milos"); // get here returns char** ?
cout << *s_ptr.get(); // get here returns char** ?
那么,虽然unique_ptr<char[]>
返回底层数组的char*
,但shared_ptr<char[]>
似乎返回char(*)[]
指向数组指针的指针? 我做了一些测试,我可以将shared_ptr<char>
与默认删除一起使用:
shared_ptr<char> s_ptr(new char[10], default_delete<char[]>());
sprint(s_ptr.get(), 10, "milos"); // get here returns char*, fine
cout << s_ptr.get(); // same here
但我猜 C++17 背后的想法不是使用shared_ptr<char>
和自定义指定删除器的方法:default_delete<char[]>
因为这在 C++17 之前是可能的。
有人可以澄清正确的用法,以及为什么会发生以下情况:
unique_ptr<int> u = make_unique<int>(5);
auto iu = u.get(); // iu is typeof int*
同时
unique_ptr<int[]> ua = make_unique<int[]>(5);
auto iua = ua.get(); // iua is still typeof int* ?
而对于shared_ptr:
shared_ptr<int> s = make_shared<int>(5);
auto is = s.get(); // is is typeof int*
但
shared_ptr<int[]> sa = make_shared<int[]>(5);
auto isa = sa.get(); // isa is typeof int** ???
.
>std::unique_ptr
对 T 是数组类型时具有特定的专用化。这会导致选择正确的默认删除程序。
无论好坏,std::shared_ptr
都没有(而std::make_shared
从 c++20 开始(。
你在shared_ptr
中看到的行为,由于调用 get(( 返回一个 T** 是一个异常,已经在 c++17 中修复了(它现在将返回 T*(。
这是因为直到 c++17 之前,std::shared_ptr::element_type
被T
并且std::shared_ptr::get
返回一个T*
。
从 c++17 开始,element_type
被定义为std::remove_extent_t<T>
,get()
返回element_type*
;
所以给定using SAI = std::shared_ptr<int[]>
在 C++14 中:
SAI::element_type = int[]
衰变成int**
SAI::get() -> (int[])*
在 C++17 中:
SAI::element_type = int
SAI::get() -> int*
供参考的文档链接:
https://en.cppreference.com/w/cpp/memory/unique_ptr https://en.cppreference.com/w/cpp/memory/shared_ptr
据我所知,在 c++17 之前创建共享数组的方法如下所示:
#include <memory>
int main()
{
auto uai = std::make_unique<int[]>(10);
std::shared_ptr<int> sai { uai.release(), uai.get_deleter() };
}
在 C++17 之后:
#include <memory>
int main()
{
std::shared_ptr<int[]> sai = std::make_unique<int[]>(10);
}