为什么std :: unique_ptr不能隐式转换为T *和const T *?

PSkocik

如果我要自己写,我想我会做些类似的事情:

template<typename T, typename Dtor = std::default_delete<T> >
class Uptr : private Dtor {
  T* vl_;
public:
  explicit Uptr(T* vl = nullptr) noexcept : vl_(vl) {}
  ~Uptr() noexcept { Dtor::operator()(vl_); }

  Uptr& swap(Uptr& o) noexcept { T* tmp; tmp = vl_; vl_=o.vl_; o.vl_ = tmp; }

  Uptr& operator=(Uptr&& o) noexcept { o.swap(*this); }
  Uptr& operator=(nullptr_t) noexcept { vl_=nullptr; return *this; } 
  Uptr(Uptr&& o) noexcept : Uptr(nullptr) { *this = std::move(o); } 

  Uptr(const Uptr& o) = delete;
  Uptr& operator=(const Uptr& o) = delete;


  operator T*() noexcept { return vl_; } 
  operator const T*() const noexcept { return vl_; } 

  T* release() noexcept { T* ret = vl_; vl_=nullptr; return ret; }    

  const Dtor& deleter() const noexcept { return *(static_cast<Dtor*>(this)); }
  Dtor& deleter() noexcept { return *(static_cast<Dtor*>(this)); }
};

而从不必定义拯救自己get()和运营商*->以及[]

在这种情况下进行隐式转换有什么问题?

阿米·塔沃里(Ami Tavory)

我认为您的问题不是特定于的unique_ptr,而是一般来说是关于智能指针的。

赫伯·萨特(Herb Sutter)很久以前就写过这本书显然,它将允许您编写逻辑错误的代码,例如:

unique_ptr<something> p;
...
delete p; // p is a smart pointer - probably not what you want.

和其他类似的代码。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

是否存在从std :: shared_ptr <T>到std :: shared_ptr <const T>的隐式转换?

来自分类Dev

添加从unique_ptr <T>到T *的隐式转换

来自分类Dev

为什么std :: shared_ptr <T> = std :: unique_ptr <T []>编译,而std :: shared_ptr <T []> = std :: unique_ptr <T []>不编译?

来自分类Dev

const std :: vector <T>和std :: vector <T> const有什么区别?

来自分类Dev

const std :: vector <T>和std :: vector <T> const有什么区别?

来自分类Dev

在std :: vector <std :: unique_ptr <T>中的const T&上迭代

来自分类Dev

为什么unique_ptr重载重设(指针p =指针())和重设(nullptr_t)?

来自分类Dev

T *和const T *

来自分类Dev

为什么const char *隐式转换为bool而不是std :: string?

来自分类Dev

为什么const char *隐式转换为bool而不是std :: string?

来自分类Dev

为什么不能互换std :: atomic <T>?

来自分类Dev

从std:unique_ptr隐式转换为布尔值时出错

来自分类Dev

为什么C#不能隐式转换Action <T>其中T:BaseType到Action <BaseType>

来自分类Dev

为什么无法将ArrayList <ArrayList <T >>隐式转换为Iterable <Iterable <T >>?

来自分类Dev

为什么unique_ptr <T> ::〜unique_ptr需要定义T?

来自分类Dev

为什么C#null可以隐式转换为System.Nullable <T>,但不能自定义Nullable <T>

来自分类Dev

不能使用std :: unique_ptr <T>且T为前向声明

来自分类Dev

std :: unique_ptr <T []>和自定义分配器删除器

来自分类Dev

为什么允许从对<int64_t,int64_t>隐式转换为对<int,int>?

来自分类Dev

unique_ptr <T>和unique_ptr <T> &&之间的区别

来自分类Dev

为什么将const rvalue引用隐式转换为const引用?

来自分类Dev

参数std :: unique_ptr <T> &&的std :: move或std :: forward

来自分类Dev

还有什么更好的替代std :: vector <std :: unique_ptr <T >>吗?

来自分类Dev

还有什么更好的替代std :: vector <std :: unique_ptr <T >>吗?

来自分类Dev

为什么不能使用const std :: random_device?

来自分类Dev

为什么将T(const T &&)称为move构造函数?

来自分类Dev

为什么在返回指针时不能将其自动转换为unique_ptr?

来自分类Dev

如何初始化std :: unique_ptr <std :: unique_ptr <T> []>?

来自分类Dev

`&mut retval`和`retval as * const T as * mut T`有什么区别?

Related 相关文章

  1. 1

    是否存在从std :: shared_ptr <T>到std :: shared_ptr <const T>的隐式转换?

  2. 2

    添加从unique_ptr <T>到T *的隐式转换

  3. 3

    为什么std :: shared_ptr <T> = std :: unique_ptr <T []>编译,而std :: shared_ptr <T []> = std :: unique_ptr <T []>不编译?

  4. 4

    const std :: vector <T>和std :: vector <T> const有什么区别?

  5. 5

    const std :: vector <T>和std :: vector <T> const有什么区别?

  6. 6

    在std :: vector <std :: unique_ptr <T>中的const T&上迭代

  7. 7

    为什么unique_ptr重载重设(指针p =指针())和重设(nullptr_t)?

  8. 8

    T *和const T *

  9. 9

    为什么const char *隐式转换为bool而不是std :: string?

  10. 10

    为什么const char *隐式转换为bool而不是std :: string?

  11. 11

    为什么不能互换std :: atomic <T>?

  12. 12

    从std:unique_ptr隐式转换为布尔值时出错

  13. 13

    为什么C#不能隐式转换Action <T>其中T:BaseType到Action <BaseType>

  14. 14

    为什么无法将ArrayList <ArrayList <T >>隐式转换为Iterable <Iterable <T >>?

  15. 15

    为什么unique_ptr <T> ::〜unique_ptr需要定义T?

  16. 16

    为什么C#null可以隐式转换为System.Nullable <T>,但不能自定义Nullable <T>

  17. 17

    不能使用std :: unique_ptr <T>且T为前向声明

  18. 18

    std :: unique_ptr <T []>和自定义分配器删除器

  19. 19

    为什么允许从对<int64_t,int64_t>隐式转换为对<int,int>?

  20. 20

    unique_ptr <T>和unique_ptr <T> &&之间的区别

  21. 21

    为什么将const rvalue引用隐式转换为const引用?

  22. 22

    参数std :: unique_ptr <T> &&的std :: move或std :: forward

  23. 23

    还有什么更好的替代std :: vector <std :: unique_ptr <T >>吗?

  24. 24

    还有什么更好的替代std :: vector <std :: unique_ptr <T >>吗?

  25. 25

    为什么不能使用const std :: random_device?

  26. 26

    为什么将T(const T &&)称为move构造函数?

  27. 27

    为什么在返回指针时不能将其自动转换为unique_ptr?

  28. 28

    如何初始化std :: unique_ptr <std :: unique_ptr <T> []>?

  29. 29

    `&mut retval`和`retval as * const T as * mut T`有什么区别?

热门标签

归档