12 template <
typename... Args>
13 [[nodiscard]]
static SharedPtr<T> make(Args&&... args) {
14 return SharedPtr<T>(
new T(std::forward<Args>(args)...));
17 [[nodiscard]] SharedPtr() noexcept : counter(
nullptr) {}
18 [[nodiscard]] SharedPtr(std::nullptr_t) noexcept : counter(
nullptr) {}
22 [[nodiscard]] SharedPtr(SharedPtr
const& other) {
23 counter = other.counter;
25 counter->addShareCount();
29 [[nodiscard]] SharedPtr(SharedPtr&& other)
noexcept {
30 counter = other.counter;
31 other.counter =
nullptr;
34 SharedPtr& operator=(SharedPtr
const& other) {
39 counter = other.counter;
41 counter->addShareCount();
47 SharedPtr& operator=(SharedPtr&& other)
noexcept {
52 counter = other.counter;
53 other.counter =
nullptr;
59 [[nodiscard]]
explicit SharedPtr(SharedPtr<Y>
const& other)
60 requires(std::convertible_to<Y*, T*>)
64 counter->addShareCount();
69 [[nodiscard]]
explicit SharedPtr(SharedPtr<Y>&& other)
70 requires(std::convertible_to<Y*, T*>)
73 other.counter =
nullptr;
77 [[nodiscard]]
explicit SharedPtr(
WeakPtr<Y> const& other)
78 requires(std::convertible_to<Y*, T*>)
82 counter->addShareCount();
93 SharedPtr<T>& operator=(SharedPtr<Y>
const& other)
94 requires(std::convertible_to<Y*, T*>)
99 counter->addShareCount();
106 SharedPtr<T>& operator=(SharedPtr<Y>&& other)
107 requires(std::convertible_to<Y*, T*>)
111 other.counter =
nullptr;
117 SharedPtr<T>& operator=(
WeakPtr<Y> const& other)
118 requires(std::convertible_to<Y*, T*>)
122 counter->addShareCount();
127 [[nodiscard]] T* get()
const {
return counter ? counter->get() :
nullptr; }
129 [[nodiscard]] T* operator->()
const {
return get(); }
131 [[nodiscard]] T& operator*()
const {
return *get(); }
133 [[nodiscard]]
explicit operator bool()
const {
return get() !=
nullptr; }
135 [[nodiscard]]
int use_count()
const {
return counter ? counter->getShareCount() : 0; }