日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++新特性11 (11)unique_ptr

發(fā)布時間:2025/3/21 c/c++ 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++新特性11 (11)unique_ptr 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • 有一個_Compressed_pair成員,類似于pair<key,value>隊
  • // CLASS TEMPLATE unique_ptr SCALAR template <class _Ty, class _Dx /* = default_delete<_Ty> */> class unique_ptr { // non-copyable pointer to an object public:using pointer = typename _Get_deleter_pointer_type<_Ty, remove_reference_t<_Dx>>::type;using element_type = _Ty;using deleter_type = _Dx;template <class _Dx2 = _Dx, _Unique_ptr_enable_default_t<_Dx2> = 0>constexpr unique_ptr() noexcept : _Mypair(_Zero_then_variadic_args_t{}) {}template <class _Dx2 = _Dx, _Unique_ptr_enable_default_t<_Dx2> = 0>constexpr unique_ptr(nullptr_t) noexcept : _Mypair(_Zero_then_variadic_args_t{}) {}unique_ptr& operator=(nullptr_t) noexcept {reset();return *this;}template <class _Dx2 = _Dx, _Unique_ptr_enable_default_t<_Dx2> = 0>explicit unique_ptr(pointer _Ptr) noexcept : _Mypair(_Zero_then_variadic_args_t{}, _Ptr) {}template <class _Dx2 = _Dx, enable_if_t<is_constructible_v<_Dx2, const _Dx2&>, int> = 0>unique_ptr(pointer _Ptr, const _Dx& _Dt) noexcept : _Mypair(_One_then_variadic_args_t{}, _Dt, _Ptr) {}template <class _Dx2 = _Dx,enable_if_t<conjunction_v<negation<is_reference<_Dx2>>, is_constructible<_Dx2, _Dx2>>, int> = 0>unique_ptr(pointer _Ptr, _Dx&& _Dt) noexcept : _Mypair(_One_then_variadic_args_t{}, _STD move(_Dt), _Ptr) {}template <class _Dx2 = _Dx,enable_if_t<conjunction_v<is_reference<_Dx2>, is_constructible<_Dx2, remove_reference_t<_Dx2>>>, int> = 0>unique_ptr(pointer, remove_reference_t<_Dx>&&) = delete;template <class _Dx2 = _Dx, enable_if_t<is_move_constructible_v<_Dx2>, int> = 0>unique_ptr(unique_ptr&& _Right) noexcept: _Mypair(_One_then_variadic_args_t{}, _STD forward<_Dx>(_Right.get_deleter()), _Right.release()) {}template <class _Ty2, class _Dx2,enable_if_t<conjunction_v<negation<is_array<_Ty2>>, is_convertible<typename unique_ptr<_Ty2, _Dx2>::pointer, pointer>,conditional_t<is_reference_v<_Dx>, is_same<_Dx2, _Dx>, is_convertible<_Dx2, _Dx>>>,int> = 0>unique_ptr(unique_ptr<_Ty2, _Dx2>&& _Right) noexcept: _Mypair(_One_then_variadic_args_t{}, _STD forward<_Dx2>(_Right.get_deleter()), _Right.release()) {}#if _HAS_AUTO_PTR_ETCtemplate <class _Ty2,enable_if_t<conjunction_v<is_convertible<_Ty2*, _Ty*>, is_same<_Dx, default_delete<_Ty>>>, int> = 0>unique_ptr(auto_ptr<_Ty2>&& _Right) noexcept : _Mypair(_Zero_then_variadic_args_t{}, _Right.release()) {} #endif // _HAS_AUTO_PTR_ETCtemplate <class _Ty2, class _Dx2,enable_if_t<conjunction_v<negation<is_array<_Ty2>>, is_assignable<_Dx&, _Dx2>,is_convertible<typename unique_ptr<_Ty2, _Dx2>::pointer, pointer>>,int> = 0>unique_ptr& operator=(unique_ptr<_Ty2, _Dx2>&& _Right) noexcept {reset(_Right.release());_Mypair._Get_first() = _STD forward<_Dx2>(_Right._Mypair._Get_first());return *this;}template <class _Dx2 = _Dx, enable_if_t<is_move_assignable_v<_Dx2>, int> = 0>unique_ptr& operator=(unique_ptr&& _Right) noexcept {if (this != _STD addressof(_Right)) {reset(_Right.release());_Mypair._Get_first() = _STD forward<_Dx>(_Right._Mypair._Get_first());}return *this;}void swap(unique_ptr& _Right) noexcept {_Swap_adl(_Mypair._Myval2, _Right._Mypair._Myval2);_Swap_adl(_Mypair._Get_first(), _Right._Mypair._Get_first());}~unique_ptr() noexcept {if (_Mypair._Myval2) {_Mypair._Get_first()(_Mypair._Myval2);}}_NODISCARD _Dx& get_deleter() noexcept {return _Mypair._Get_first();}_NODISCARD const _Dx& get_deleter() const noexcept {return _Mypair._Get_first();}_NODISCARD add_lvalue_reference_t<_Ty> operator*() const noexcept /* strengthened */ {return *_Mypair._Myval2;}_NODISCARD pointer operator->() const noexcept {return _Mypair._Myval2;}_NODISCARD pointer get() const noexcept {return _Mypair._Myval2;}explicit operator bool() const noexcept {return static_cast<bool>(_Mypair._Myval2);}pointer release() noexcept {return _STD exchange(_Mypair._Myval2, nullptr);}void reset(pointer _Ptr = nullptr) noexcept {pointer _Old = _STD exchange(_Mypair._Myval2, _Ptr);if (_Old) {_Mypair._Get_first()(_Old);}}unique_ptr(const unique_ptr&) = delete;unique_ptr& operator=(const unique_ptr&) = delete;private:template <class, class>friend class unique_ptr;_Compressed_pair<_Dx, pointer> _Mypair; };

    總結(jié)

    以上是生活随笔為你收集整理的c++新特性11 (11)unique_ptr的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。