Skip to content

Commit 81cedac

Browse files
mordantephilnik777
andauthored
[libc++] Deprecates and removes shared_ptr::unqiue. (#76576)
The status table incorrectly marks P0521R0 as nothing to do. This is not correct the function should be deprecated. During our latest monthly meeting we argreed to remove the _LIBCPP_ENABLE_CXXyy_REMOVED_FEATURES macros, therefore the new macro is not added to that global list. Implements - P0521R0 Proposed Resolution for CA 14 (shared_ptr use_count/unique) Implements parts of - P0619R4 Reviewing Deprecated Facilities of C++17 for C++20 --------- Co-authored-by: Nikolas Klauser <[email protected]>
1 parent bd3d358 commit 81cedac

File tree

9 files changed

+63
-5
lines changed

9 files changed

+63
-5
lines changed

libcxx/docs/ReleaseNotes/18.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Implemented Papers
5757
- P2871R3 - Remove Deprecated Unicode Conversion Facets from C++26
5858
- P2870R3 - Remove basic_string::reserve()
5959
- P2909R4 - Fix formatting of code units as integers (Dude, where’s my ``char``?)
60+
- P0521R0 - Proposed Resolution for CA 14 (shared_ptr use_count/unique)
6061

6162

6263
Improvements and New Features
@@ -83,6 +84,9 @@ Improvements and New Features
8384
- The ``_LIBCPP_ENABLE_CXX26_REMOVED_STRING_RESERVE`` macro has been added to make
8485
the function ``std::basic_string<...>::reserve()`` available.
8586

87+
- The ``_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE`` macro has been added to make
88+
the function ``std::shared_ptr<...>::unique()`` available.
89+
8690

8791
Deprecations and Removals
8892
-------------------------

libcxx/docs/Status/Cxx17Papers.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"`P0513R0 <https://wg21.link/P0513R0>`__","LWG","Poisoning the Hash","Issaquah","|Complete|","5.0"
8888
"`P0516R0 <https://wg21.link/P0516R0>`__","LWG","Clarify That shared_future's Copy Operations have Wide Contracts","Issaquah","|Complete|","4.0"
8989
"`P0517R0 <https://wg21.link/P0517R0>`__","LWG","Make future_error Constructible","Issaquah","|Complete|","4.0"
90-
"`P0521R0 <https://wg21.link/P0521R0>`__","LWG","Proposed Resolution for CA 14 (shared_ptr use_count/unique)","Issaquah","|Nothing To Do|","n/a"
90+
"`P0521R0 <https://wg21.link/P0521R0>`__","LWG","Proposed Resolution for CA 14 (shared_ptr use_count/unique)","Issaquah","|Complete|","18.0"
9191
"","","","","",""
9292
"`P0156R2 <https://wg21.link/P0156R2>`__","LWG","Variadic Lock guard(rev 5)","Kona","|Complete|","5.0"
9393
"`P0270R3 <https://wg21.link/P0270R3>`__","CWG","Removing C dependencies from signal handler wording","Kona","",""

libcxx/docs/Status/Cxx20.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Paper Status
4444
.. [#note-P0645] P0645: The paper is implemented but still marked as an incomplete feature
4545
(the feature-test macro is not set).
4646
.. [#note-P0966] P0966: It was previously erroneously marked as complete in version 8.0. See `bug 45368 <https://llvm.org/PR45368>`__.
47-
.. [#note-P0619] P0619: Only sections D.8, D.9, D.10 and D.13 are implemented. Sections D.4, D.7, D.11, D.12, and D.14 remain undone.
47+
.. [#note-P0619] P0619: Only sections D.8, D.9, D.10 and D.13 are implemented. Sections D.4, D.7, D.11, and D.12 remain undone.
4848
.. [#note-P0883.1] P0883: shared_ptr and floating-point changes weren't applied as they themselves aren't implemented yet.
4949
.. [#note-P0883.2] P0883: ``ATOMIC_FLAG_INIT`` was marked deprecated in version 14.0, but was undeprecated with the implementation of LWG3659 in version 15.0.
5050
.. [#note-P2231] P2231: Optional is complete. The changes to variant haven't been implemented yet.

libcxx/docs/UsingLibcxx.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ C++17 Specific Configuration Macros
296296

297297
C++20 Specific Configuration Macros
298298
-----------------------------------
299+
**_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE**
300+
This macro is used to re-enable the function
301+
``std::shared_ptr<...>::unique()``.
302+
299303
**_LIBCPP_ENABLE_CXX20_REMOVED_FEATURES**:
300304
This macro is used to re-enable all the features removed in C++20. The effect
301305
is equivalent to manually defining each macro listed below.

libcxx/include/__memory/shared_ptr.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,9 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
723723

724724
_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __cntrl_ ? __cntrl_->use_count() : 0; }
725725

726-
_LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT { return use_count() == 1; }
726+
#if _LIBCPP_STD_VER < 20 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE)
727+
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI bool unique() const _NOEXCEPT { return use_count() == 1; }
728+
#endif
727729

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

libcxx/include/memory

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ public:
629629
T& operator*() const noexcept;
630630
T* operator->() const noexcept;
631631
long use_count() const noexcept;
632-
bool unique() const noexcept;
632+
bool unique() const noexcept; // deprected in C++17, removed in C++20
633633
explicit operator bool() const noexcept;
634634
template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
635635
template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE
12+
13+
// <memory>
14+
15+
// shared_ptr
16+
17+
// bool unique() const; // deprecated in C++17, removed in C++20
18+
19+
#include <memory>
20+
21+
void f() {
22+
const std::shared_ptr<int> p;
23+
p.unique(); // expected-warning {{'unique' is deprecated}}
24+
}

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.obs/unique.pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
// shared_ptr
1212

13-
// bool unique() const;
13+
// bool unique() const; // deprecated in C++17, removed in C++20
14+
15+
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS -D_LIBCPP_ENABLE_CXX20_REMOVED_SHARED_PTR_UNIQUE
1416

1517
#include <memory>
1618
#include <cassert>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
11+
// <memory>
12+
13+
// shared_ptr
14+
15+
// bool unique() const; // deprecated in C++17, removed in C++20
16+
17+
#include <memory>
18+
19+
void f() {
20+
const std::shared_ptr<int> p;
21+
p.unique(); // expected-error {{no member named 'unique' in 'std::shared_ptr<int>}}
22+
}

0 commit comments

Comments
 (0)