Document number | P0414R0 |
Date | 2015-07-07 |
Project | Programming Language C++, Library Working Group |
Reply-to | Jonathan Wakely <cxx@kayari.org> |
LWG Motion 6 in Jacksonville was to apply to the C++ working paper
the wording from P0220R1,
Adopt Library Fundamentals V2 TS Components for C++17 (R1),
but due to conflicts with changes in the C++ WP the shared_ptr
changes
were not applied.
I offered to write this paper showing the changes relative to the current WP. I failed to do so before Oulu in time for the CD, but provide it now.
Changes are relative to N4594. Clauses have been renumbered since that draft, so cross-references are given using [stable.names] for clarity.
Modify the class synopsis in 20.10.2.2 Class template shared_ptr
[util.smartptr.shared]:
namespace std {
template<class T> class shared_ptr {
public:
typedef T element_type;
using element_type = remove_extent_t<T>;
[...]
// 20.10.2.2.5, observers:
Telement_type* get() const noexcept;
T& operator*() const noexcept;
T* operator->() const noexcept;
element_type& operator[](ptrdiff_t i) const noexcept;
long use_count() const noexcept;
bool unique() const noexcept;
explicit operator bool() const noexcept;
template<class U> bool owner_before(shared_ptr<U> const& b) const;
template<class U> bool owner_before(weak_ptr<U> const& b) const;
};
[...]
// 20.10.2.2.9, shared_ptr casts:
template<class T, class U>
shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
template<class T, class U>
shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept;
template<class T, class U>
shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r) noexcept;
template<class T, class U>
shared_ptr<T> reinterpret_pointer_cast(const shared_ptr<U>& r) noexcept;
Add a new paragraph at the end of 20.10.2.2 Class template shared_ptr
[util.smartptr.shared]:
-5- For the purposes of subclause [util.smartptr], a pointer type
Y*
is said to be compatible with a pointer typeT*
when eitherY*
is convertible toT*
orY
isU[N]
andT
isU
cv[]
.
Change 20.10.2.2.1 shared_ptr
constructors [util.smartptr.shared.const]:
template<class Y> explicit shared_ptr(Y* p);
-4- _Requires:
p shall be convertible to T*.Y
shall be a complete type. The expressiondelete p
delete[] p
, whenT
is an array type, ordelete p
, whenT
is not an array type, shall be well formed, shall have well defined behavior, and shall not throw exceptions. WhenT
isU[N]
,Y(*)[N]
shall be convertible toT*
; whenT
isU[]
,Y(*)[]
shall be convertible toT*
; otherwise,Y*
shall be convertible toT*
.-5- Effects:
CWhenT
is not an array type, constructs ashared_ptr
object that owns the pointerp
. Otherwise, constructs ashared_ptr
that ownsp
and a deleter of an unspecified type that callsdelete[] p
. Enablesshared_from_this
withp
. If an exception is thrown,delete p
is called whenT
is not an array type,delete[] p
otherwise.-6- Postconditions:
use_count() == 1 && get() == p
.-7- Throws:
bad_alloc
, or an implementation-defined exception when a resource other than memory could not be obtained.template<class Y, class D> shared_ptr(Y* p, D d); template<class Y, class D, class A> shared_ptr(Y* p, D d, A a); template <class D> shared_ptr(nullptr_t p, D d); template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
-8- Requires:
p
shall be convertible toT*
.D
shall beCopyConstructible
. The copy constructor and destructor ofD
shall not throw exceptions. The expressiond(p)
shall be well formed, shall have well defined behavior, and shall not throw exceptions.A
shall be an allocator (17.6.3.5). The copy constructor and destructor ofA
shall not throw exceptions. WhenT
isU[N]
,Y(*)[N]
shall be convertible toT*
; whenT
isU[]
,Y(*)[]
shall be convertible toT*
; otherwise,Y*
shall be convertible toT*
.-9- Effects: Constructs a
shared_ptr
object that owns the objectp
and the deleterd
. The first and second constructors enableshared_from_this
withp
. The second and fourth constructors shall use a copy ofa
to allocate memory for internal use. If an exception is thrown,d(p)
is called.-10- Postconditions:
use_count() == 1 && get() == p
.-11- Throws:
bad_alloc
, or an implementation-defined exception when a resource other than memory could not be obtained.template<class Y> shared_ptr(const shared_ptr<Y>& r,
Telement_type* p) noexcept;-12- Effects: [...]
-13- Postconditions: [...]
-14- [Note: [...]
-15- [Note: [...]
shared_ptr(const shared_ptr& r) noexcept; template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
-16- Remark: The second constructor shall not participate in overload resolution unless
Y*
isimplicitly convertible tocompatible withT*
.-17- Effects: [...]
-18- Postconditions: [...]
shared_ptr(shared_ptr&& r) noexcept; template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
-19- Remark: The second constructor shall not participate in overload resolution unless
Y*
isconvertible tocompatible withT*
.-20- Effects: [...]
-21- Postconditions: [...]
template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
-22- Requires:
Y*
shall beconvertible tocompatible withT*
.-23- Effects: [...]
-24- Postconditions: [...]
-25- Throws: [...]
template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
-26- Remark: This constructor shall not participate in overload resolution unless
unique_ptr<Y, D>::pointer
isconvertible tocompatible withT*
.-27 Effects: [...]
Change 20.10.2.2.5 shared_ptr
observers [util.smartptr.shared.obs]:
Telement_type* get() const noexcept;-1- Returns: the stored pointer.
T& operator*() const noexcept;
-2- Requires:
get() != 0
.-3- Returns:
*get()
.-4- Remarks: When
T
is an array type or (possibly cv-qualified)void
, it is unspecified whether this member function is declared. If it is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function shall be well formed.T* operator->() const noexcept;
-5- Requires:
get() != 0
.-6- Returns:
get()
.-7- When
T
is an array type or (possibly cv-qualified)void
, it is unspecified whether this member function is declared. If it is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function shall be well formed.element_type& operator[](ptrdiff_t i) const noexcept;
-?- Requires:
get() != 0 && i >= 0
. IfT
isU[N]
,i < N
.-?- Returns:
get()[i]
.-?- Remarks: When
T
is not an array type, it is unspecified whether this member function is declared. If it is declared, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function shall be well formed.
Change 20.10.2.2.9 shared_ptr
casts [util.smartptr.shared.cast]:
template<class T, class U> shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept;
-1- Requires: The expression
static_cast<T*>(
shall be well formed.r.get()(U*)0)-2- Returns:
shared_ptr<T>(r, static_cast<typename shared_ptr<T>::element_type*>(r.get()))
.Ifr
is empty, an emptyshared_ptr<T>
; otherwise, ashared_ptr<T>
object that storesstatic_cast<T*>(r.get())
and shares ownership withr
.
-3- Postconditions:w.get() == static_cast<T*>(r.get())
andw.use_count() == r.use_count()
, wherew
is the return value.-4- [Note: The seemingly equivalent expression
shared_ptr<T>(static_cast<T*>(r.get()))
will eventually result in undefined behavior, attempting to delete the same object twice. --end note]template<class T, class U> shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept;
-5- Requires: The expression
dynamic_cast<T*>(
shall be well formed and shall have well defined behavior.r.get()(U*)0)-6- Returns:
— Whendynamic_cast<
returns a nonzero valueTtypename shared_ptr<T>::element_type*>(r.get()), ashared_ptr<T>
object that stores a copy of it and shares ownership withr
p
,shared_ptr<T>(r, p)
;
— Otherwise,an emptyshared_ptr<T>()
object.
-7- Postcondition:w.get() == dynamic_cast<T*>(r.get())
, wherew
is the return value.-8- [Note: The seemingly equivalent expression shared_ptr
(dynamic_cast<T*>(r.get())) will eventually result in undefined behavior, attempting to delete the same object twice. --end note] template<class T, class U> shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r) noexcept;
-9- Requires: The expression
const_cast<T*>(
shall be well formed.r.get()(U*)0)-10- Returns:
shared_ptr<T>(r, const_cast<typename shared_ptr<T>::element_type*>(r.get()))
.Ifr
is empty, an emptyshared_ptr<T>
; otherwise, ashared_ptr<T>
object that storesconst_cast<T*>(r.get())
and shares ownership withr
.
-11- Postconditions:w.get() == const_cast<T*>(r.get())
andw.use_count() == r.use_count()
, wherew
is the return value.-12- [Note: The seemingly equivalent expression
shared_ptr<T>(const_cast<T*>(r.get()))
will eventually result in undefined behavior, attempting to delete the same object twice. --end note]template<class T, class U> shared_ptr<T> reinterpret_pointer_cast(const shared_ptr<U>& r) noexcept;
-?- Requires: The expression
reinterpret_cast<T*>((U*)0)
shall be well formed.-?- Returns:
shared_ptr<T>(r, reinterpret_cast<typename shared_ptr<T>::element_type*>(r.get()))
.
Change 20.10.2.3.1 weak_ptr
constructors [util.smartptr.weak.const]:
weak_ptr(const weak_ptr& r) noexcept; template<class Y> weak_ptr(const weak_ptr<Y>& r) noexcept; template<class Y> weak_ptr(const shared_ptr<Y>& r) noexcept;
-3- Remark: The second and third constructors shall not participate in overload resolution unless
Y*
isimplicitly convertible tocompatible withT*
.