Skip to content

Commit b2266e0

Browse files
committed
[libc++][memory] Applied [[nodiscard]] to smart pointers
Applied `[[nodiscard]]` where relevant to smart pointers and related functions. See guidelines: - https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant - `[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. For example a locking constructor in unique_lock.
1 parent 35a95fe commit b2266e0

File tree

3 files changed

+204
-55
lines changed

3 files changed

+204
-55
lines changed

libcxx/include/__memory/shared_ptr.h

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -568,16 +568,20 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
568568
shared_ptr(__p, __d, __a).swap(*this);
569569
}
570570

571-
_LIBCPP_HIDE_FROM_ABI element_type* get() const _NOEXCEPT { return __ptr_; }
571+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI element_type* get() const _NOEXCEPT { return __ptr_; }
572572

573-
_LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator*() const _NOEXCEPT { return *__ptr_; }
573+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator*() const _NOEXCEPT {
574+
return *__ptr_;
575+
}
574576

575577
_LIBCPP_HIDE_FROM_ABI element_type* operator->() const _NOEXCEPT {
576578
static_assert(!is_array<_Tp>::value, "std::shared_ptr<T>::operator-> is only valid when T is not an array type.");
577579
return __ptr_;
578580
}
579581

580-
_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; }
582+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT {
583+
return __cntrl_ ? __cntrl_->use_count() : 0;
584+
}
581585

582586
#if _LIBCPP_STD_VER < 20 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE)
583587
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT { return use_count() == 1; }
@@ -586,19 +590,19 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
586590
_LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return get() != nullptr; }
587591

588592
template <class _Up>
589-
_LIBCPP_HIDE_FROM_ABI bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT {
593+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT {
590594
return __cntrl_ < __p.__cntrl_;
591595
}
592596

593597
template <class _Up>
594-
_LIBCPP_HIDE_FROM_ABI bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT {
598+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT {
595599
return __cntrl_ < __p.__cntrl_;
596600
}
597601

598602
_LIBCPP_HIDE_FROM_ABI bool __owner_equivalent(const shared_ptr& __p) const { return __cntrl_ == __p.__cntrl_; }
599603

600604
#if _LIBCPP_STD_VER >= 17
601-
_LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const {
605+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const {
602606
static_assert(is_array<_Tp>::value, "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
603607
return __ptr_[__i];
604608
}
@@ -669,7 +673,7 @@ shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
669673
// std::allocate_shared and std::make_shared
670674
//
671675
template <class _Tp, class _Alloc, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0>
672-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&... __args) {
676+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&... __args) {
673677
using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
674678
using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
675679
__allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
@@ -680,21 +684,21 @@ _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&
680684
}
681685

682686
template <class _Tp, class... _Args, __enable_if_t<!is_array<_Tp>::value, int> = 0>
683-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(_Args&&... __args) {
687+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(_Args&&... __args) {
684688
return std::allocate_shared<_Tp>(allocator<__remove_cv_t<_Tp> >(), std::forward<_Args>(__args)...);
685689
}
686690

687691
#if _LIBCPP_STD_VER >= 20
688692

689693
template <class _Tp, class _Alloc, __enable_if_t<!is_array<_Tp>::value, int> = 0>
690-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
694+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
691695
using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
692696
_ForOverwriteAllocator __alloc(__a);
693697
return std::allocate_shared<_Tp>(__alloc);
694698
}
695699

696700
template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
697-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
701+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
698702
return std::allocate_shared_for_overwrite<_Tp>(allocator<__remove_cv_t<_Tp>>());
699703
}
700704

@@ -886,67 +890,69 @@ _LIBCPP_HIDE_FROM_ABI shared_ptr<_Array> __allocate_shared_bounded_array(const _
886890

887891
// bounded array variants
888892
template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
889-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a) {
893+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a) {
890894
return std::__allocate_shared_bounded_array<_Tp>(__a);
891895
}
892896

893897
template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
894-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) {
898+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
899+
allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) {
895900
return std::__allocate_shared_bounded_array<_Tp>(__a, __u);
896901
}
897902

898903
template <class _Tp, class _Alloc, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
899-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
904+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
900905
using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
901906
_ForOverwriteAllocator __alloc(__a);
902907
return std::__allocate_shared_bounded_array<_Tp>(__alloc);
903908
}
904909

905910
template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
906-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared() {
911+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared() {
907912
return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>());
908913
}
909914

910915
template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
911-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u) {
916+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u) {
912917
return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u);
913918
}
914919

915920
template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
916-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
921+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
917922
return std::__allocate_shared_bounded_array<_Tp>(allocator<__for_overwrite_tag>());
918923
}
919924

920925
// unbounded array variants
921926
template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
922-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n) {
927+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n) {
923928
return std::__allocate_shared_unbounded_array<_Tp>(__a, __n);
924929
}
925930

926931
template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
927-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u) {
932+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
933+
allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u) {
928934
return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u);
929935
}
930936

931937
template <class _Tp, class _Alloc, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
932-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a, size_t __n) {
938+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a, size_t __n) {
933939
using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
934940
_ForOverwriteAllocator __alloc(__a);
935941
return std::__allocate_shared_unbounded_array<_Tp>(__alloc, __n);
936942
}
937943

938944
template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
939-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n) {
945+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n) {
940946
return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n);
941947
}
942948

943949
template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
944-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u) {
950+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u) {
945951
return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u);
946952
}
947953

948954
template <class _Tp, __enable_if_t<is_unbounded_array<_Tp>::value, int> = 0>
949-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite(size_t __n) {
955+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite(size_t __n) {
950956
return std::__allocate_shared_unbounded_array<_Tp>(allocator<__for_overwrite_tag>(), __n);
951957
}
952958

@@ -980,12 +986,14 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator>(const shared_ptr<_Tp>& __x, const sh
980986
}
981987

982988
template <class _Tp, class _Up>
983-
inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
989+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool
990+
operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
984991
return !(__y < __x);
985992
}
986993

987994
template <class _Tp, class _Up>
988-
inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
995+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI bool
996+
operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT {
989997
return !(__x < __y);
990998
}
991999

@@ -1075,21 +1083,23 @@ inline _LIBCPP_HIDE_FROM_ABI void swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __
10751083
}
10761084

10771085
template <class _Tp, class _Up>
1078-
inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
1086+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
1087+
static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
10791088
return shared_ptr<_Tp>(__r, static_cast< typename shared_ptr<_Tp>::element_type*>(__r.get()));
10801089
}
10811090

10821091
// LWG-2996
10831092
// We don't backport because it is an evolutionary change.
10841093
#if _LIBCPP_STD_VER >= 20
10851094
template <class _Tp, class _Up>
1086-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1095+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> static_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
10871096
return shared_ptr<_Tp>(std::move(__r), static_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
10881097
}
10891098
#endif
10901099

10911100
template <class _Tp, class _Up>
1092-
inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
1101+
[[__nodiscard__]] inline
1102+
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
10931103
typedef typename shared_ptr<_Tp>::element_type _ET;
10941104
_ET* __p = dynamic_cast<_ET*>(__r.get());
10951105
return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
@@ -1099,14 +1109,14 @@ inline _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(const shared_p
10991109
// We don't backport because it is an evolutionary change.
11001110
#if _LIBCPP_STD_VER >= 20
11011111
template <class _Tp, class _Up>
1102-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1112+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> dynamic_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
11031113
auto* __p = dynamic_cast<typename shared_ptr<_Tp>::element_type*>(__r.get());
11041114
return __p ? shared_ptr<_Tp>(std::move(__r), __p) : shared_ptr<_Tp>();
11051115
}
11061116
#endif
11071117

11081118
template <class _Tp, class _Up>
1109-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
1119+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
11101120
typedef typename shared_ptr<_Tp>::element_type _RTp;
11111121
return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
11121122
}
@@ -1115,29 +1125,29 @@ _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(const shared_ptr<_Up>&
11151125
// We don't backport because it is an evolutionary change.
11161126
#if _LIBCPP_STD_VER >= 20
11171127
template <class _Tp, class _Up>
1118-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1128+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> const_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
11191129
return shared_ptr<_Tp>(std::move(__r), const_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
11201130
}
11211131
#endif
11221132

11231133
template <class _Tp, class _Up>
1124-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
1134+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT {
11251135
return shared_ptr<_Tp>(__r, reinterpret_cast< typename shared_ptr<_Tp>::element_type*>(__r.get()));
11261136
}
11271137

11281138
// LWG-2996
11291139
// We don't backport because it is an evolutionary change.
11301140
#if _LIBCPP_STD_VER >= 20
11311141
template <class _Tp, class _Up>
1132-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
1142+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> reinterpret_pointer_cast(shared_ptr<_Up>&& __r) noexcept {
11331143
return shared_ptr<_Tp>(std::move(__r), reinterpret_cast<typename shared_ptr<_Tp>::element_type*>(__r.get()));
11341144
}
11351145
#endif
11361146

11371147
#if _LIBCPP_HAS_RTTI
11381148

11391149
template <class _Dp, class _Tp>
1140-
inline _LIBCPP_HIDE_FROM_ABI _Dp* get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT {
1150+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _Dp* get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT {
11411151
return __p.template __get_deleter<_Dp>();
11421152
}
11431153

@@ -1192,15 +1202,19 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI weak_ptr {
11921202
_LIBCPP_HIDE_FROM_ABI void swap(weak_ptr& __r) _NOEXCEPT;
11931203
_LIBCPP_HIDE_FROM_ABI void reset() _NOEXCEPT;
11941204

1195-
_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; }
1196-
_LIBCPP_HIDE_FROM_ABI bool expired() const _NOEXCEPT { return __cntrl_ == nullptr || __cntrl_->use_count() == 0; }
1197-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> lock() const _NOEXCEPT;
1205+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT {
1206+
return __cntrl_ ? __cntrl_->use_count() : 0;
1207+
}
1208+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool expired() const _NOEXCEPT {
1209+
return __cntrl_ == nullptr || __cntrl_->use_count() == 0;
1210+
}
1211+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> lock() const _NOEXCEPT;
11981212
template <class _Up>
1199-
_LIBCPP_HIDE_FROM_ABI bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT {
1213+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT {
12001214
return __cntrl_ < __r.__cntrl_;
12011215
}
12021216
template <class _Up>
1203-
_LIBCPP_HIDE_FROM_ABI bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT {
1217+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT {
12041218
return __cntrl_ < __r.__cntrl_;
12051219
}
12061220

@@ -1384,13 +1398,15 @@ class enable_shared_from_this {
13841398
_LIBCPP_HIDE_FROM_ABI ~enable_shared_from_this() {}
13851399

13861400
public:
1387-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> shared_from_this() { return shared_ptr<_Tp>(__weak_this_); }
1388-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp const> shared_from_this() const { return shared_ptr<const _Tp>(__weak_this_); }
1401+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> shared_from_this() { return shared_ptr<_Tp>(__weak_this_); }
1402+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp const> shared_from_this() const {
1403+
return shared_ptr<const _Tp>(__weak_this_);
1404+
}
13891405

13901406
#if _LIBCPP_STD_VER >= 17
1391-
_LIBCPP_HIDE_FROM_ABI weak_ptr<_Tp> weak_from_this() _NOEXCEPT { return __weak_this_; }
1407+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI weak_ptr<_Tp> weak_from_this() _NOEXCEPT { return __weak_this_; }
13921408

1393-
_LIBCPP_HIDE_FROM_ABI weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT { return __weak_this_; }
1409+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT { return __weak_this_; }
13941410
#endif // _LIBCPP_STD_VER >= 17
13951411

13961412
template <class _Up>

libcxx/include/__memory/unique_ptr.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,17 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI unique_ptr {
258258
return *this;
259259
}
260260

261-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator*() const
261+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __add_lvalue_reference_t<_Tp> operator*() const
262262
_NOEXCEPT_(_NOEXCEPT_(*std::declval<pointer>())) {
263263
return *__ptr_;
264264
}
265265
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer operator->() const _NOEXCEPT { return __ptr_; }
266-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer get() const _NOEXCEPT { return __ptr_; }
267-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 deleter_type& get_deleter() _NOEXCEPT { return __deleter_; }
268-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const deleter_type& get_deleter() const _NOEXCEPT {
266+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 pointer get() const _NOEXCEPT { return __ptr_; }
267+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 deleter_type& get_deleter() _NOEXCEPT {
268+
return __deleter_;
269+
}
270+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 const deleter_type&
271+
get_deleter() const _NOEXCEPT {
269272
return __deleter_;
270273
}
271274
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 explicit operator bool() const _NOEXCEPT {
@@ -672,8 +675,8 @@ operator<=>(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {
672675
#endif
673676

674677
template <class _T1, class _D1>
675-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool
676-
operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT {
678+
inline _LIBCPP_HIDE_FROM_ABI
679+
_LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT {
677680
return !__x;
678681
}
679682

@@ -748,12 +751,13 @@ operator<=>(const unique_ptr<_T1, _D1>& __x, nullptr_t) {
748751
#if _LIBCPP_STD_VER >= 14
749752

750753
template <class _Tp, class... _Args, enable_if_t<!is_array<_Tp>::value, int> = 0>
751-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique(_Args&&... __args) {
754+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI
755+
_LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique(_Args&&... __args) {
752756
return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...));
753757
}
754758

755759
template <class _Tp, enable_if_t<__is_unbounded_array_v<_Tp>, int> = 0>
756-
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique(size_t __n) {
760+
[[__nodiscard__]] inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique(size_t __n) {
757761
typedef __remove_extent_t<_Tp> _Up;
758762
return unique_ptr<_Tp>(__private_constructor_tag(), new _Up[__n](), __n);
759763
}
@@ -766,12 +770,13 @@ void make_unique(_Args&&...) = delete;
766770
#if _LIBCPP_STD_VER >= 20
767771

768772
template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
769-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique_for_overwrite() {
773+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique_for_overwrite() {
770774
return unique_ptr<_Tp>(new _Tp);
771775
}
772776

773777
template <class _Tp, enable_if_t<is_unbounded_array_v<_Tp>, int> = 0>
774-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp> make_unique_for_overwrite(size_t __n) {
778+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unique_ptr<_Tp>
779+
make_unique_for_overwrite(size_t __n) {
775780
return unique_ptr<_Tp>(__private_constructor_tag(), new __remove_extent_t<_Tp>[__n], __n);
776781
}
777782

0 commit comments

Comments
 (0)