MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
shared_ptr.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_MEMORY_SHARED_PTR_HPP__
2#define MSTL_CORE_MEMORY_SHARED_PTR_HPP__
3
11
16#include <new>
18
24
27
34struct __smart_ptr_counter {
35public:
36 _MSTL atomic_ulong strong_count_;
37 _MSTL atomic_ulong weak_count_;
38
39protected:
43 virtual void delete_object() noexcept = 0;
44
48 virtual void delete_this() noexcept {
49 delete this;
50 }
51
52public:
57 __smart_ptr_counter() noexcept
58 : strong_count_(1), weak_count_(1) {}
59
60 __smart_ptr_counter(__smart_ptr_counter&&) = delete;
61
62 virtual ~__smart_ptr_counter() = default;
63
67 void incref_strong() noexcept {
68 strong_count_.fetch_add(1, _MSTL memory_order_relaxed);
69 }
70
74 void incref_weak() noexcept {
75 weak_count_.fetch_add(1, _MSTL memory_order_relaxed);
76 }
77
82 void decref_strong() noexcept {
83 if (strong_count_.fetch_sub(1, _MSTL memory_order_acq_rel) == 1) {
84 delete_object();
85 decref_weak();
86 }
87 }
88
93 void decref_weak() noexcept {
94 if (weak_count_.fetch_sub(1, _MSTL memory_order_acq_rel) == 1) {
95 delete_this();
96 }
97 }
98
105 bool try_incref_strong() noexcept {
106 auto strong = strong_count_.load(_MSTL memory_order_relaxed);
107 do {
108 if (strong == 0) return false;
109 } while (!strong_count_.compare_exchange_weak(
110 strong, strong + 1,
113 ));
114 return true;
115 }
116
121 MSTL_NODISCARD uint64_t use_count() const noexcept {
122 return strong_count_.load(_MSTL memory_order_relaxed);
123 }
124
129 MSTL_NODISCARD uint64_t weak_count() const noexcept {
130 return weak_count_.load(_MSTL memory_order_relaxed);
131 }
132};
133
141template <typename T, typename Deleter>
142struct __smart_ptr_counter_impl final : __smart_ptr_counter {
143 T* ptr_;
144 Deleter deleter_;
145
146 explicit __smart_ptr_counter_impl(T* ptr) noexcept
147 : ptr_(ptr) {}
148
149 explicit __smart_ptr_counter_impl(T* ptr, Deleter&& deleter) noexcept
150 : ptr_(ptr), deleter_(_MSTL forward<Deleter>(deleter)) {}
151
152 void delete_object() noexcept override {
153 deleter_(ptr_);
154 ptr_ = nullptr;
155 }
156};
157
165template <typename T, typename Deleter>
166struct __smart_ptr_counter_impl_fused final : __smart_ptr_counter {
167 T* ptr_;
168 void* mem_;
169 Deleter deleter_;
170
171 explicit __smart_ptr_counter_impl_fused(T* ptr, void* mem, Deleter deleter) noexcept
172 : ptr_(ptr), mem_(mem), deleter_(_MSTL move(deleter)) {}
173
174 void delete_object() noexcept override {
175 deleter_(ptr_);
176 ptr_ = nullptr;
177 }
178
179 void delete_this() noexcept override {
180 operator delete(mem_
181#if MSTL_STANDARD_17__
182 , static_cast<std::align_val_t>(
183 _MSTL max(alignof(T), alignof(__smart_ptr_counter_impl_fused)))
184#endif
185 );
186 }
187};
188
197template <typename T, typename Deleter, typename Alloc>
198struct __smart_ptr_counter_impl_allocated final : __smart_ptr_counter {
199 T* ptr_;
200 void* mem_;
201 size_t size_;
202 Deleter deleter_;
203 Alloc allocator_;
204
205 explicit __smart_ptr_counter_impl_allocated(
206 T* ptr, void* mem, const size_t size,
207 Deleter deleter, Alloc alloc) noexcept
208 : ptr_(ptr), mem_(mem), size_(size),
209 deleter_(_MSTL move(deleter)),
210 allocator_(_MSTL move(alloc)) {}
211
212 void delete_object() noexcept override {
213 deleter_(ptr_);
214 ptr_ = nullptr;
215 }
216
217 void delete_this() noexcept override {
218 using alloc_traits = allocator_traits<Alloc>;
219 using byte_allocator = typename alloc_traits::template alloc_rebind_t<Alloc, byte_t>;
220 byte_allocator byte_alloc(allocator_);
221 allocator_traits<byte_allocator>::deallocate(byte_alloc, static_cast<byte_t*>(mem_), size_);
222 }
223};
224
227
228
229template <typename T>
231
232template <typename T>
233class shared_ptr;
234
235template <typename T>
236class weak_ptr;
237
240
241template <typename T>
243__setup_enable_shared_from(T* ptr, __smart_ptr_counter* owner) {
244 static_cast<_MSTL enable_shared_from_this<T>*>(ptr)->owner_ = owner;
245 return;
246}
247
248template <typename T>
250__setup_enable_shared_from(T*, __smart_ptr_counter*) {
251 return;
252}
253
254template <typename T>
255MSTL_ALWAYS_INLINE shared_ptr<T> __make_shared_fused(T* ptr, __smart_ptr_counter* owner) noexcept {
256 return _MSTL shared_ptr<T>(ptr, owner);
257}
258
261
262
271template <typename T>
272class shared_ptr {
273public:
274 using element_type = T;
275
276private:
277 element_type* ptr_ = nullptr;
278 _INNER __smart_ptr_counter* owner_ = nullptr;
279
285 explicit shared_ptr(T* ptr, _INNER __smart_ptr_counter* owner) noexcept
286 : ptr_(ptr), owner_(owner) {}
287
288 template <typename U>
289 friend class shared_ptr;
290
291 template <typename U>
292 friend class weak_ptr;
293
294 template <typename U>
295 friend shared_ptr<U> _INNER __make_shared_fused(U*, _INNER __smart_ptr_counter*) noexcept;
296
297public:
304 shared_ptr(nullptr_t np = nullptr) noexcept {}
305
311 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
313 : ptr_(ptr), owner_(new _INNER __smart_ptr_counter_impl<U, default_delete<U>>(ptr)) {
314 _INNER __setup_enable_shared_from<T>(ptr_, owner_);
315 }
316
324 template <typename U, typename Deleter, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
325 explicit shared_ptr(U* ptr, Deleter&& deleter)
326 : ptr_(ptr), owner_(new _INNER __smart_ptr_counter_impl<U, Deleter>(ptr, _MSTL forward<Deleter>(deleter))) {
327 _INNER __setup_enable_shared_from<T>(ptr_, owner_);
328 }
329
336 template <typename U, typename Deleter, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
338 : shared_ptr(ptr.release(), ptr.get_deleter()) {}
339
344 shared_ptr(const shared_ptr& x) noexcept
345 : ptr_(x.ptr_), owner_(x.owner_) {
346 if (owner_) owner_->incref_strong();
347 }
348
354 shared_ptr& operator =(const shared_ptr& x) noexcept {
355 if (_MSTL addressof(x) == this) return *this;
356 if (owner_) owner_->decref_strong();
357 ptr_ = x.ptr_;
358 owner_ = x.owner_;
359 if (owner_) owner_->incref_strong();
360 return *this;
361 }
362
368 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
369 shared_ptr(const shared_ptr<U>& x) noexcept
370 : ptr_(x.ptr_), owner_(x.owner_) {
371 if (owner_) owner_->incref_strong();
372 }
373
378 shared_ptr(shared_ptr&& x) noexcept
379 : ptr_(x.ptr_), owner_(x.owner_) {
380 x.ptr_ = nullptr;
381 x.owner_ = nullptr;
382 }
383
389 shared_ptr& operator =(shared_ptr&& x) noexcept {
390 if (_MSTL addressof(x) == this) return *this;
391 if (owner_) owner_->decref_strong();
392 ptr_ = x.ptr_;
393 owner_ = x.owner_;
394 x.ptr_ = nullptr;
395 x.owner_ = nullptr;
396 return *this;
397 }
398
404 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
405 explicit shared_ptr(shared_ptr<U>&& x) noexcept
406 : ptr_(x.ptr_), owner_(x.owner_) {
407 x.ptr_ = nullptr;
408 x.owner_ = nullptr;
409 }
410
418 template <typename U>
419 shared_ptr(const shared_ptr<U>& x, T* ptr) noexcept
420 : ptr_(ptr), owner_(x.owner_) {
421 if (owner_) owner_->incref_strong();
422 }
423
427 template <typename U>
428 shared_ptr(shared_ptr<U>&& x, T* ptr) noexcept
429 : ptr_(ptr), owner_(x.owner_) {
430 x.ptr_ = nullptr;
431 x.owner_ = nullptr;
432 }
433
437 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
438 shared_ptr& operator =(const shared_ptr<U>& x) noexcept {
439 if (owner_) owner_->decref_strong();
440 ptr_ = x.ptr_;
441 owner_ = x.owner_;
442 if (owner_) owner_->incref_strong();
443 return *this;
444 }
445
449 template <typename U, enable_if_t<is_convertible_v<U*, T*>, int> = 0>
450 shared_ptr& operator =(shared_ptr<U>&& x) noexcept {
451 if (owner_) owner_->decref_strong();
452 ptr_ = x.ptr_;
453 owner_ = x.owner_;
454 x.ptr_ = nullptr;
455 x.owner_ = nullptr;
456 return *this;
457 }
458
463 ~shared_ptr() noexcept {
464 reset();
465 }
466
470 void reset() noexcept {
471 if (owner_) owner_->decref_strong();
472 owner_ = nullptr;
473 ptr_ = nullptr;
474 }
475
481 template <typename U>
482 void reset(U* ptr) {
483 if (owner_) owner_->decref_strong();
484 ptr_ = nullptr;
485 owner_ = nullptr;
486 ptr_ = ptr;
487 owner_ = new _INNER __smart_ptr_counter_impl<U, default_delete<U>>(ptr);
488 _INNER __setup_enable_shared_from<T>(ptr_, owner_);
489 }
490
498 template <typename U, typename Deleter>
499 void reset(U* ptr, Deleter deleter) {
500 if (owner_) owner_->decref_strong();
501 ptr_ = nullptr;
502 owner_ = nullptr;
503 ptr_ = ptr;
504 owner_ = new _INNER __smart_ptr_counter_impl<U, Deleter>(ptr, _MSTL move(deleter));
505 _INNER __setup_enable_shared_from<T>(ptr_, owner_);
506 }
507
512 MSTL_NODISCARD long use_count() const noexcept {
513 return owner_ ? owner_->use_count() : 0;
514 }
515
520 MSTL_NODISCARD bool unique() const noexcept {
521 return owner_ ? owner_->use_count() == 1 : true;
522 }
523
528 void swap(shared_ptr& x) noexcept {
529 if (_MSTL addressof(x) == this) return;
530 _MSTL swap(ptr_, x.ptr_);
531 _MSTL swap(owner_, x.owner_);
532 }
533
538 MSTL_NODISCARD T* get() const noexcept {
539 return ptr_;
540 }
541
546 MSTL_NODISCARD T* operator ->() const noexcept {
547 return ptr_;
548 }
549
554 MSTL_NODISCARD add_lvalue_reference_t<T> operator *() const noexcept {
555 return *ptr_;
556 }
557
562 MSTL_NODISCARD explicit operator bool() const noexcept {
563 return ptr_ != nullptr;
564 }
565
572 template <typename U>
573 MSTL_NODISCARD bool owner_equal(const shared_ptr<U>& rhs) const noexcept {
574 return owner_ == rhs.owner_;
575 }
576
580 template <typename U>
581 MSTL_NODISCARD bool owner_equal(const weak_ptr<U>& rhs) const noexcept {
582 return owner_ == rhs.owner_;
583 }
584
591 template <typename U>
592 MSTL_NODISCARD bool owner_before(const shared_ptr<U>& rhs) const noexcept {
593 return owner_ < rhs.owner_;
594 }
595
599 template <typename U>
600 MSTL_NODISCARD bool owner_before(const weak_ptr<U>& rhs) const noexcept {
601 return owner_ < rhs.owner_;
602 }
603};
604
608template <typename T, typename U>
609MSTL_NODISCARD bool operator ==(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept {
610 return lhs.owner_equal(rhs);
611}
612
616template <typename T, typename U>
617MSTL_NODISCARD bool operator !=(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept {
618 return !(lhs == rhs);
619}
620
624template <typename T, typename U>
625MSTL_NODISCARD bool operator <(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept {
626 return lhs.owner_before(rhs);
627}
628
632template <typename T, typename U>
633MSTL_NODISCARD bool operator >(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept {
634 return rhs < lhs;
635}
636
640template <typename T, typename U>
641MSTL_NODISCARD bool operator <=(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept {
642 return !(lhs > rhs);
643}
644
648template <typename T, typename U>
649MSTL_NODISCARD bool operator >=(const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept {
650 return !(lhs < rhs);
651}
652
653
658template <typename T>
659class shared_ptr<T[]> : shared_ptr<T> {
660public:
661 using shared_ptr<T>::shared_ptr;
662
668 add_lvalue_reference_t<T> operator [](size_t idx) {
669 return this->get()[idx];
670 }
671};
672
673
681template <typename T>
683private:
684 _INNER __smart_ptr_counter* owner_ = nullptr;
685
686 template <typename U>
687 friend void __set_enable_shared_from(enable_shared_from_this<U>*, _INNER __smart_ptr_counter*);
688
689protected:
694
701 static_assert(is_base_of_v<enable_shared_from_this, T>, "shared from T requires derived class");
702 if (!owner_) {
703 throw_exception(memory_exception("smart pointer share failed."));
704 }
705 owner_->incref_strong();
706 return _INNER __make_shared_fused(static_cast<T*>(this), owner_);
707 }
708
715 static_assert(is_base_of_v<enable_shared_from_this, T>, "shared from T requires derived class");
716 if (!owner_) {
717 throw_exception(memory_exception("smart pointer share failed."));
718 }
719 owner_->incref_strong();
720 return _INNER __make_shared_fused(static_cast<T const*>(this), owner_);
721 }
722};
723
724
735template <typename T, typename... Args>
736enable_if_t<!is_unbounded_array_v<T> && is_constructible_v<T, Args...>, shared_ptr<T>>
737make_shared(Args&&... args) {
738 auto const deleter = [](T* ptr) noexcept { ptr->~T(); };
739 using Counter = _INNER __smart_ptr_counter_impl_fused<T, decltype(deleter)>;
740 constexpr size_t align = _MSTL max(alignof(T), alignof(Counter));
741 constexpr size_t offset = (sizeof(Counter) + align - 1) & ~(align - 1);
742 constexpr size_t size = offset + sizeof(T);
743#if MSTL_STANDARD_17__
744 void* mem = ::operator new(size, static_cast<std::align_val_t>(align));
745 Counter* counter = static_cast<Counter*>(mem);
746#else
747 void* mem = ::operator new(size + align - 1);
748 size_t aligned_addr = (reinterpret_cast<size_t>(mem) + (align - 1)) & ~(align - 1);
749 Counter* counter = reinterpret_cast<Counter*>(aligned_addr);
750#endif
751 T* object = reinterpret_cast<T*>(reinterpret_cast<byte_t*>(counter) + offset);
752 try {
753 _MSTL construct(object, _MSTL forward<Args>(args)...);
754 } catch (...) {
755#if MSTL_STANDARD_17__
756 operator delete(mem, static_cast<std::align_val_t>(align));
757#else
758 operator delete(mem);
759#endif
760 throw_exception(memory_exception("shared ptr construction failed."));
761 }
762 new (counter) Counter(object, mem, deleter);
763 _INNER __setup_enable_shared_from(object, counter);
764 return _INNER __make_shared_fused(object, counter);
765}
766
774template <typename T>
776make_shared(const size_t len) {
777 using value = remove_extent_t<T>;
778 auto* tmp = new value[len]();
779 try {
780 return shared_ptr<T>(tmp);
781 } catch (...) {
782 delete[] tmp;
783 throw_exception(memory_exception("shared ptr construction failed."));
784 }
785 MSTL_UNREACHABLE;
786}
787
798template <typename T, typename Alloc, typename... Args>
799enable_if_t<!is_array_v<T> && is_constructible_v<T, Args...>, shared_ptr<T>>
800allocate_shared(Alloc& alloc, Args&&... args) {
801 auto deleter = [](T* p) { p->~T(); };
802 using ControlBlock = _INNER __smart_ptr_counter_impl_allocated<T, decltype(deleter), Alloc>;
803
804 const size_t align = _MSTL max(alignof(ControlBlock), alignof(T));
805 const size_t offset = (sizeof(ControlBlock) + align - 1) & ~(align - 1);
806 const size_t total_size = offset + sizeof(T);
807 const size_t raw_size = total_size + align - 1;
808
809 using alloc_traits = allocator_traits<remove_cv_t<Alloc>>;
810 using byte_allocator = typename alloc_traits::template alloc_rebind_t<Alloc, byte_t>;
811 byte_allocator byte_alloc(alloc);
812
813 byte_t* raw_mem = allocator_traits<byte_allocator>::allocate(byte_alloc, raw_size);
814
815 const uintptr_t raw_addr = reinterpret_cast<uintptr_t>(raw_mem);
816 const uintptr_t aligned_addr = (raw_addr + align - 1) & ~static_cast<uintptr_t>(align - 1);
817 auto aligned_mem = reinterpret_cast<byte_t*>(aligned_addr);
818 T* object_ptr = reinterpret_cast<T*>(aligned_mem + offset);
819
820 try {
821 allocator_traits<Alloc>::construct(alloc, object_ptr, _MSTL forward<Args>(args)...);
822 } catch (...) {
823 allocator_traits<byte_allocator>::deallocate(byte_alloc, raw_mem, raw_size);
824 throw_exception(memory_exception("shared ptr ref object construction failed."));
825 }
826
827 ControlBlock* ctrl_block = nullptr;
828 try {
829 ctrl_block = ::new (aligned_mem) ControlBlock(object_ptr, raw_mem, raw_size, deleter, alloc);
830 } catch (...) {
831 allocator_traits<Alloc>::destroy(alloc, object_ptr);
832 allocator_traits<byte_allocator>::deallocate(byte_alloc, raw_mem, raw_size);
833 throw_exception(memory_exception("shared ptr control block construction failed."));
834 }
835
836 _INNER __setup_enable_shared_from(object_ptr, ctrl_block);
837 return _INNER __make_shared_fused(object_ptr, ctrl_block);
838}
839
840
848template <typename T, typename U>
850 return shared_ptr<T>(ptr, static_cast<T*>(ptr.get()));
851}
852
860template <typename T, typename U>
862 return shared_ptr<T>(ptr, const_cast<T*>(ptr.get()));
863}
864
872template <typename T, typename U>
874 return shared_ptr<T>(ptr, reinterpret_cast<T*>(ptr.get()));
875}
876
884template <typename T, typename U>
886 T* tmp = dynamic_cast<T*>(ptr.get());
887 if (tmp != nullptr) return shared_ptr<T>(ptr, tmp);
888 return nullptr;
889}
890
891
892template <typename T>
893struct hash<shared_ptr<T>> {
894 MSTL_CONSTEXPR20 size_t operator ()(const shared_ptr<T>& ptr) const
895 noexcept(noexcept(_MSTL declval<_MSTL hash<T*>>()(_MSTL declval<T*>()))) {
896 return hash<T*>()(ptr.get());
897 }
898};
899 // SharedPointer
901
903#endif // MSTL_CORE_MEMORY_SHARED_PTR_HPP__
MSTL分配器特性
MSTL原子类型完整实现
共享智能指针类模板
shared_ptr(const shared_ptr< U > &x, T *ptr) noexcept
从共享指针和别名指针别名构造函数
MSTL_NODISCARD bool owner_before(const shared_ptr< U > &rhs) const noexcept
比较所有权顺序
shared_ptr(shared_ptr &&x) noexcept
移动构造函数
void swap(shared_ptr &x) noexcept
交换两个共享指针
MSTL_NODISCARD long use_count() const noexcept
获取引用计数
shared_ptr & operator=(const shared_ptr &x) noexcept
拷贝赋值运算符
shared_ptr(shared_ptr< U > &&x, T *ptr) noexcept
移动别名构造函数
void reset(U *ptr)
重置共享指针并管理新对象
shared_ptr(const shared_ptr< U > &x) noexcept
类型转换拷贝构造函数
shared_ptr(nullptr_t np=nullptr) noexcept
默认构造函数
MSTL_NODISCARD add_lvalue_reference_t< T > operator*() const noexcept
解引用运算符
MSTL_NODISCARD bool owner_equal(const weak_ptr< U > &rhs) const noexcept
与弱指针检查所有权是否相等
shared_ptr(U *ptr, Deleter &&deleter)
从原始指针和自定义删除器构造函数
MSTL_NODISCARD T * operator->() const noexcept
指针解引用运算符
~shared_ptr() noexcept
析构函数
void reset(U *ptr, Deleter deleter)
带自定义删除器重置共享指针并管理新对象
shared_ptr(shared_ptr< U > &&x) noexcept
类型转换移动构造函数
MSTL_NODISCARD bool unique() const noexcept
检查是否独占所有权
void reset() noexcept
重置共享指针
MSTL_NODISCARD T * get() const noexcept
获取原始指针
shared_ptr(U *ptr)
从原始指针构造函数
shared_ptr(unique_ptr< U, Deleter > &&ptr)
独享智能指针构造函数
MSTL_NODISCARD bool owner_before(const weak_ptr< U > &rhs) const noexcept
与弱指针比较所有权顺序
MSTL_NODISCARD bool owner_equal(const shared_ptr< U > &rhs) const noexcept
检查所有权是否相等
shared_ptr(const shared_ptr &x) noexcept
拷贝构造函数
独占智能指针
弱智能指针类模板
MSTL比较算法
typename add_reference< T >::lvalue add_lvalue_reference_t
add_lvalue_reference的便捷别名
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
MSTL_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
atomic< unsigned long > atomic_ulong
无符号长整型原子类型
constexpr const T & max(const T &a, const T &b, Compare comp) noexcept(noexcept(comp(a, b)))
返回两个值中的较大者
unsigned char byte_t
字节类型,定义为无符号字符
unsigned long long uint64_t
64位无符号整数类型
decltype(nullptr) nullptr_t
空指针类型
add_rvalue_reference_t< T > declval() noexcept
获取类型的右值引用,仅用于非求值上下文
MSTL_CONSTEXPR20 enable_if_t< is_constructible_v< T, Args... >, void * > construct(T *ptr, Args &&... args) noexcept(is_nothrow_constructible_v< T, Args... >)
在指定内存位置构造对象
MSTL_INLINE17 constexpr auto memory_order_release
释放内存顺序常量
MSTL_INLINE17 constexpr auto memory_order_acq_rel
获取-释放内存顺序常量
MSTL_INLINE17 constexpr auto memory_order_relaxed
宽松内存顺序常量
@ release
释放操作,确保前面的读写不会被重排到后面
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
uint64_t uintptr_t
可容纳指针的无符号整数类型
typename remove_extent< T >::type remove_extent_t
remove_extent的便捷别名
MSTL_NODISCARD bool operator<=(const shared_ptr< T > &lhs, const shared_ptr< U > &rhs) noexcept
小于等于比较运算符
MSTL_NODISCARD bool operator!=(const shared_ptr< T > &lhs, const shared_ptr< U > &rhs) noexcept
不等比较运算符
MSTL_NODISCARD bool operator>(const shared_ptr< T > &lhs, const shared_ptr< U > &rhs) noexcept
大于比较运算符
MSTL_NODISCARD bool operator>=(const shared_ptr< T > &lhs, const shared_ptr< U > &rhs) noexcept
大于等于比较运算符
MSTL_NODISCARD bool operator<(const shared_ptr< T > &lhs, const shared_ptr< U > &rhs) noexcept
小于比较运算符(基于所有权顺序)
shared_ptr< T > const_pointer_cast(const shared_ptr< U > &ptr)
CV类型转换
MSTL_NODISCARD bool operator==(const shared_ptr< T > &lhs, const shared_ptr< U > &rhs) noexcept
相等比较运算符
shared_ptr< T > dynamic_pointer_cast(const shared_ptr< U > &ptr)
动态类型转换
enable_if_t<!is_unbounded_array_v< T > &&is_constructible_v< T, Args... >, shared_ptr< T > > make_shared(Args &&... args)
融合分配创建共享指针
enable_if_t<!is_array_v< T > &&is_constructible_v< T, Args... >, shared_ptr< T > > allocate_shared(Alloc &alloc, Args &&... args)
使用分配器创建共享指针
shared_ptr< T > static_pointer_cast(const shared_ptr< U > &ptr)
静态类型转换
shared_ptr< T > reinterpret_pointer_cast(const shared_ptr< U > &ptr)
重解释类型转换
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
void swap()=delete
删除无参数的swap重载
MSTL_NODISCARD MSTL_ALWAYS_INLINE constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
分配器特性模板
static MSTL_CONSTEXPR20 void destroy(Alloc &alloc, T *ptr) noexcept(noexcept(allocator_traits::__destroy_aux(alloc, ptr, 0)))
销毁对象
static MSTL_NODISCARD MSTL_CONSTEXPR20 pointer allocate(Alloc &alloc, size_type n)
分配内存
static MSTL_CONSTEXPR20 void deallocate(Alloc &alloc, pointer ptr, size_type n)
释放内存
static MSTL_CONSTEXPR20 void construct(Alloc &alloc, T *ptr, Args &&... args) noexcept(noexcept(allocator_traits::__construct_aux(alloc, ptr, _MSTL forward< Args >(args)...)))
在已分配内存上构造对象
MSTL_ALWAYS_INLINE value_type fetch_add(value_type value, const memory_order mo=memory_order_seq_cst) noexcept
原子获取并添加操作
默认删除器
启用从this创建共享指针的基类
enable_shared_from_this() noexcept
构造函数
shared_ptr< T const > shared_from_this() const
获取指向自身的常量共享指针
shared_ptr< T > shared_from_this()
获取指向自身的共享指针
哈希函数的主模板
内存操作异常
MSTL独占智能指针