[英]C++17: Difference in pointer storage between unique_ptr<char[]> and shared_ptr<char[]>
I am getting confused with: 我感到困惑:
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*
And: 和:
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** ?
So, while unique_ptr<char[]>
returns char*
for the underlying array, shared_ptr<char[]>
seems to return char(*)[]
pointer to array pointer? 因此,虽然unique_ptr<char[]>
返回底层数组的char*
,但shared_ptr<char[]>
似乎返回char(*)[]
指向数组指针的指针? I made few tests and I could use shared_ptr<char>
with default delete: 我进行了一些测试,并且可以将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
But I'm guessing the idea behind C++17 is not the approach with shared_ptr<char>
and custom specified deleter: default_delete<char[]>
cause this was possible prior to C++17. 但是我猜想C ++ 17背后的想法不是使用shared_ptr<char>
和自定义指定删除器的方法: default_delete<char[]>
因为这可能在C ++ 17之前出现。
Can someone clarify correct usage, and why does following occurs: 有人可以澄清正确的用法,为什么会发生以下情况:
unique_ptr<int> u = make_unique<int>(5);
auto iu = u.get(); // iu is typeof int*
at the same time 与此同时
unique_ptr<int[]> ua = make_unique<int[]>(5);
auto iua = ua.get(); // iua is still typeof int* ?
While for shared_ptr: 而对于shared_ptr:
shared_ptr<int> s = make_shared<int>(5);
auto is = s.get(); // is is typeof int*
but 但
shared_ptr<int[]> sa = make_shared<int[]>(5);
auto isa = sa.get(); // isa is typeof int** ???
. 。
std::unique_ptr
has a specific specialisation for when T is an array type. 当T为数组类型时, std::unique_ptr
具有特定的专长。 This causes the correct default deleter to be selected. 这将导致选择正确的默认删除器。
For better or worse, std::shared_ptr
does not (while std::make_shared
does as of c++20). 无论好坏, std::shared_ptr
都没有(而std::make_shared
从c ++ 20开始std::make_shared
)。
The behaviour you see in shared_ptr
, returning a T** as a result of calling get() is an anomaly which has been fixed in c++17 (it will now return T*). 您在shared_ptr
看到的行为(由于调用get()而返回T **)是一个异常,已在c ++ 17中修复(现在将返回T *)。
This is because until c++17, std::shared_ptr::element_type
was T
and std::shared_ptr::get
returned a T*
. 这是因为直到c ++ 17为止, std::shared_ptr::element_type
都是T
而std::shared_ptr::get
返回T*
。
As of c++17 element_type
is defined to be std::remove_extent_t<T>
and get()
returns element_type*
; 从c ++ 17开始, element_type
被定义为std::remove_extent_t<T>
并且get()
返回element_type*
;
so given using SAI = std::shared_ptr<int[]>
因此using SAI = std::shared_ptr<int[]>
in c++14: 在c ++ 14中:
SAI::element_type = int[]
SAI::get() -> (int[])*
which decays to int**
SAI::get() -> (int[])*
衰减为int**
in c++17: 在c ++ 17中:
SAI::element_type = int
SAI::get() -> int*
Documentation links for reference: 参考文档链接:
https://en.cppreference.com/w/cpp/memory/unique_ptr https://en.cppreference.com/w/cpp/memory/shared_ptr https://en.cppreference.com/w/cpp/memory/unique_ptr https://en.cppreference.com/w/cpp/memory/shared_ptr
As far as I am aware, the way to create a shared array prior to c++17 would be something like this: 据我所知,在c ++ 17之前创建共享数组的方法如下:
#include <memory>
int main()
{
auto uai = std::make_unique<int[]>(10);
std::shared_ptr<int> sai { uai.release(), uai.get_deleter() };
}
and after c++17: 在c ++ 17之后:
#include <memory>
int main()
{
std::shared_ptr<int[]> sai = std::make_unique<int[]>(10);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.