简体   繁体   English

C ++ 17:unique_ptr之间的指针存储差异 <char[]> 和shared_ptr <char[]>

[英]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都是Tstd::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.

相关问题 C++ 入门第 5 版:shared_ptr 和 unique_ptr 的删除器之间的区别 - C++ primer 5th edition: Difference between the deleter of a shared_ptr and of unique_ptr's 可升级指针(unique_ptr - &gt; shared_ptr) - Upgradeable pointer (unique_ptr -> shared_ptr) 打破shared_ptr和unique_ptr之间的循环依赖关系 - Breaking a circular dependency between a shared_ptr and a unique_ptr 将shared_ptr的嵌套智能指针重置为shared_ptr(或为unique_ptr),似乎是自相矛盾的 - Resetting nested smart pointer of a shared_ptr to a shared_ptr (or to a unique_ptr), seeming paradox std :: unique_ptr和std :: shared_ptr之间的破坏行为差异的基本原理是什么? - What is the rationale for the difference in destruction behavior between std::unique_ptr and std::shared_ptr? Shared_ptr和unique_ptr有异常 - Shared_ptr and unique_ptr with exception C ++将类的此指针分配给unique_ptr或shared_ptr - C++ Assigning this pointer of a class to either a unique_ptr or a shared_ptr C++ 构建错误 — unique_ptr 到 shared_ptr - C++ build error — unique_ptr to shared_ptr shared_ptr或unique_ptr到CustomDialogEx - shared_ptr or unique_ptr to CustomDialogEx c++17 在容器中的 unique_ptr 之间正确移动资源 - c++17 correct moving resource between unique_ptr in containers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM