@@ -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//
671677template <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
682688template <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
689695template <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
696702template <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
888894template <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
893899template <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
898905template <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
905912template <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
910917template <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
915922template <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
921928template <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
926933template <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
931939template <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
938946template <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
943951template <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
948956template <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
10771085template <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
10851094template <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
10911100template <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
11011111template <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
11081118template <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
11171127template <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
11231133template <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
11311141template <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
11391149template <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
13861400public:
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 >
0 commit comments