Skip to content

Commit 3f151a3

Browse files
H-G-HristovZingam
andauthored
[libc++][memory] Applied [[nodiscard]] to smart pointers (#168483)
Applied `[[nodiscard]]` where relevant to smart pointers and related functions. - [x] - `std::unique_ptr` - [x] - `std::shared_ptr` - [x] - `std::weak_ptr` 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. --------- Co-authored-by: Hristo Hristov <[email protected]>
1 parent fda20d9 commit 3f151a3

File tree

4 files changed

+198
-52
lines changed

4 files changed

+198
-52
lines changed

libcxx/include/__memory/shared_ptr.h

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -568,37 +568,43 @@ 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)
583-
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT { return use_count() == 1; }
587+
[[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT {
588+
return use_count() == 1;
589+
}
584590
#endif
585591

586592
_LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return get() != nullptr; }
587593

588594
template <class _Up>
589-
_LIBCPP_HIDE_FROM_ABI bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT {
595+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT {
590596
return __cntrl_ < __p.__cntrl_;
591597
}
592598

593599
template <class _Up>
594-
_LIBCPP_HIDE_FROM_ABI bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT {
600+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT {
595601
return __cntrl_ < __p.__cntrl_;
596602
}
597603

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

600606
#if _LIBCPP_STD_VER >= 17
601-
_LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const {
607+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<element_type> operator[](ptrdiff_t __i) const {
602608
static_assert(is_array<_Tp>::value, "std::shared_ptr<T>::operator[] is only valid when T is an array type.");
603609
return __ptr_[__i];
604610
}
@@ -669,7 +675,7 @@ shared_ptr(unique_ptr<_Tp, _Dp>) -> shared_ptr<_Tp>;
669675
// std::allocate_shared and std::make_shared
670676
//
671677
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) {
678+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&... __args) {
673679
using _ControlBlock = __shared_ptr_emplace<_Tp, _Alloc>;
674680
using _ControlBlockAllocator = typename __allocator_traits_rebind<_Alloc, _ControlBlock>::type;
675681
__allocation_guard<_ControlBlockAllocator> __guard(__a, 1);
@@ -680,21 +686,21 @@ _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, _Args&&
680686
}
681687

682688
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) {
689+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(_Args&&... __args) {
684690
return std::allocate_shared<_Tp>(allocator<__remove_cv_t<_Tp> >(), std::forward<_Args>(__args)...);
685691
}
686692

687693
#if _LIBCPP_STD_VER >= 20
688694

689695
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) {
696+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
691697
using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
692698
_ForOverwriteAllocator __alloc(__a);
693699
return std::allocate_shared<_Tp>(__alloc);
694700
}
695701

696702
template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
697-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
703+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
698704
return std::allocate_shared_for_overwrite<_Tp>(allocator<__remove_cv_t<_Tp>>());
699705
}
700706

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

887893
// bounded array variants
888894
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) {
895+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a) {
890896
return std::__allocate_shared_bounded_array<_Tp>(__a);
891897
}
892898

893899
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) {
900+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
901+
allocate_shared(const _Alloc& __a, const remove_extent_t<_Tp>& __u) {
895902
return std::__allocate_shared_bounded_array<_Tp>(__a, __u);
896903
}
897904

898905
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) {
906+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a) {
900907
using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
901908
_ForOverwriteAllocator __alloc(__a);
902909
return std::__allocate_shared_bounded_array<_Tp>(__alloc);
903910
}
904911

905912
template <class _Tp, __enable_if_t<is_bounded_array<_Tp>::value, int> = 0>
906-
_LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared() {
913+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared() {
907914
return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>());
908915
}
909916

910917
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) {
918+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(const remove_extent_t<_Tp>& __u) {
912919
return std::__allocate_shared_bounded_array<_Tp>(allocator<_Tp>(), __u);
913920
}
914921

915922
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() {
923+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite() {
917924
return std::__allocate_shared_bounded_array<_Tp>(allocator<__for_overwrite_tag>());
918925
}
919926

920927
// unbounded array variants
921928
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) {
929+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared(const _Alloc& __a, size_t __n) {
923930
return std::__allocate_shared_unbounded_array<_Tp>(__a, __n);
924931
}
925932

926933
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) {
934+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp>
935+
allocate_shared(const _Alloc& __a, size_t __n, const remove_extent_t<_Tp>& __u) {
928936
return std::__allocate_shared_unbounded_array<_Tp>(__a, __n, __u);
929937
}
930938

931939
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) {
940+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> allocate_shared_for_overwrite(const _Alloc& __a, size_t __n) {
933941
using _ForOverwriteAllocator = __allocator_traits_rebind_t<_Alloc, __for_overwrite_tag>;
934942
_ForOverwriteAllocator __alloc(__a);
935943
return std::__allocate_shared_unbounded_array<_Tp>(__alloc, __n);
936944
}
937945

938946
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) {
947+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n) {
940948
return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n);
941949
}
942950

943951
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) {
952+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared(size_t __n, const remove_extent_t<_Tp>& __u) {
945953
return std::__allocate_shared_unbounded_array<_Tp>(allocator<_Tp>(), __n, __u);
946954
}
947955

948956
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) {
957+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI shared_ptr<_Tp> make_shared_for_overwrite(size_t __n) {
950958
return std::__allocate_shared_unbounded_array<_Tp>(allocator<__for_overwrite_tag>(), __n);
951959
}
952960

@@ -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: 13 additions & 8 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 {
@@ -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)