unique_ptr templated assignment
This paper addresses the Library Working Group Issue 2228.
The crux of this issue is that the converting move assignment operator of
unique_ptr will participate in overload resolution, even if the
actual implementation of unique_ptr move assignment operator will
not compile.
For example:
#include <memory>
#include <type_traits>
struct do_nothing
{
template <class T>
void operator()(T*) {}
};
int
main()
{
int i = 0;
std::unique_ptr<int, do_nothing> p1(&i);
std::unique_ptr<int> p2;
// This mistakenly compiles:
static_assert(std::is_assignable<decltype(p2), decltype(p1)>::value, "");
// But this correctly does not compile:
p2 = std::move(p1);
}
The static_assert compiles because of the Remarks: clause
of the unique_ptr converting move assignment operator in
[unique.ptr.single.asgn]:
template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;Remarks: This operator shall not participate in overload resolution unless:
unique_ptr<U, E>::pointeris implicitly convertible topointerandUis not an array type.
In our example both unique_ptr<U, E>::pointer and pointer
have type int*, and so the first bullet is satisfied. And U
has type int, which is not an array type, and so the second bullet
is also satisfied. Thus this signature shall participate in overload resolution,
and subsequently, as far as is_assignable is concerned, the move
assignment in our example above looks well formed.
However the trouble comes when we look at the Effects: clause of this operator:
template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;Effects: Transfers ownership from
uto*thisas if by callingreset(u.release())followed byget_deleter() = std::forward<E>(u.get_deleter()).
The Effects: clause is correct, but the trouble is with the assignment
of the two deleter types. In our example get_deleter() is an
lvalue expression of type std::default_delete<int>,
E has type do_nothing, and
u.get_deleter() is an lvalue expression of type
do_nothing.
std::default_delete<int> can not be assigned to by a
do_nothing. And thus even though the standard trait
is_assignable says we can move assign p1 to p2,
when we try to, we get an error from within the implementation of the std::lib.
With clang/libc++ the error looks like:
libcxx/include/memory:2563:33: error: no viable overloaded '='
__ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The complaint of LWG 2228
is right on the money: This is an intolerable situation. This is a defect in
the unique_ptr specification.
LWG 2228 currently proposes to fix this bug by adding the following additional constraint to the Remarks: clause:
template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;Remarks: This operator shall not participate in overload resolution unless:
unique_ptr<U, E>::pointeris implicitly convertible topointerandUis not an array type., and- either
Dis a reference type andEis the same type asD, orDis not a reference type andEis implicitly convertible toD.
And indeed, when we apply this extra constraint, this fixes our example
above! Neither D nor E are reference types.
However E (do_nothing) is not implicitly
convertible to D (std::default_delete<int>),
and so our static_assert now correctly refuses to compile.
However there is a problem with this fix. The big picture problem is that
the constraint does not describe what the implementation actually needs to
do. It instead describes a constraint that is in fact applicable only to the
unique_ptr converting move constructor. It happens to work in
our first example above. However in Appendix 1
a more complicated example is presented which the current proposed wording
breaks.
The current proposed wording fails to compile the currently valid, motivating, and non-contrived example in Appendix 1. The error message is:
test.cpp:130:12: error: no viable overloaded '='
pb = std::move(pd);
~~ ^ ~~~~~~~~~~~~~
The reason it fails to compile this example is because of the constraint that if both deleters are reference types, they must be the same type. But in this example our two deleter types are:
deleter<base>&deleter<derived>&
Even though the author of deleter went to the trouble to allow a
converting copy assignment from deleter<derived> to
deleter<base> (and not vice-versa), and even though this is
exactly what the specification of unique_ptr says it will do, the
current proposed wording breaks this valid, motivating, non-contrived, and
potentially already-existing-in-the-wild example.
To introduce the correct fix, it is helpful to go back to the "big picture" problem mentioned much earlier in this paper:
The big picture problem is that the constraint does not describe what the implementation actually needs to do.
What does the implementation actually need to do?
get_deleter() = std::forward<E>(u.get_deleter())
This is the current specification from the Effects: clause and there is no dispute that this part of the specification is correct. And creating a constraint for exactly this specification is easy to both implement and specify:
Change [unique.ptr.single.asgn]:
template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;Remarks: This operator shall not participate in overload resolution unless:
unique_ptr<U, E>::pointeris implicitly convertible topointerandUis not an array type., andis_assignable<D&, E&&>::valueis true.
This additional constraint correctly handles both the non-reference deleter case
and the reference deleter case. It constrains precisely what the Effects:
clause specifies — no more, and no less. This means that no existing
compiling code will cease compiling. The only impact the client will see is
that is_assignable will correctly model the unique_ptr
converting move assignment operator. That is, it will answer true in all
existing cases that compile, and it will answer false in cases that already do
not compile.
unique_ptr<T[]> Needs The Fix Too
unique_ptr<T[]> has the same defect for the same reasons as
the primary template. The only difference is that an incorrect fix has already
been applied to the current working draft, and is corrected herein:
Change [unique.ptr.runtime.asgn]:
template <class U, class E> unique_ptr& operator=(unique_ptr<U,E>&& u)noexcept;This operator behaves the same as in the primary template, except that it shall not participate in overload resolution unless all of the following conditions hold, where
UPisunique_ptr<U, E>:
Uis an array type, andpointeris the same type aselement_type*, andUP::pointeris the same type asUP::element_type*, andUP::element_type(*)[]is convertible toelement_type(*)[], andeitherDis a reference type andEis the same type asD, orDis not a reference type andEis implicitly convertible toD.is_assignable<D&, E&&>::valueis true.[ Note: this replaces the overload-resolution specification of the primary template — end note ]
Consider a stateful deleter which does nothing but count the number of times it is used to delete something. Furthermore we want the deleter to be templated on the (static) type it is deleting.
This example is more complicated than it needs to be to just demonstrate the problem. However I wanted to present an example that was not contrived. This is intended to be a realistic and motivating example. I did not want to present a contrived example and be met with arguments such as:
Nobody would ever do that. And if they tried, they would be better off if we disallowed it.
So if you accept on face value that this is a motivating, non-contrived example, you do not need to understand the details of this example to understand why the current proposal breaks this example, nor why the alternative wording proposed herein fixes it.
First the code for the deleter, and then a bit of explanation about it.
template <class T>
class deleter
{
// count the number of times this deleter is used
unsigned count_ = 0;
public:
// special members
~deleter() {count_ = std::numeric_limits<unsigned>::max();}
deleter() = default;
deleter(const deleter&) {}
deleter& operator=(const deleter&) {return *this;}
deleter(deleter&& d)
: count_(d.count_)
{
d.count_ = 0;
}
deleter& operator=(deleter&& d)
{
count_ = d.count_;
d.count_ = 0;
return *this;
}
// converting constructors
template <class U,
class = std::enable_if_t<std::is_convertible<U*, T*>::value>>
deleter(const deleter<U>& d)
{}
template <class U,
class = std::enable_if_t<std::is_convertible<U*, T*>::value>>
deleter(deleter<U>&& d)
: count_(d.count_)
{
d.count_ = 0;
}
// deletion operator
void operator()(T* t)
{
delete t;
++count_;
}
// observers
unsigned count() const {return count_;}
friend
std::ostream&
operator<<(std::ostream& os, const deleter& d)
{
return os << d.count_;
}
template <class> friend class deleter;
};
The deleter holds a count of the number of times it is used.
For debugging purposes, the destructor sets this count to an unrealistic value,
which is not guaranteed to stick around or be observable after ~deleter(),
but often is in practice.
An invariant of this class is that even across copies and moves, the total number of times a delete actually happens is the same as the sum of the counts from all of the deleters. And that invariant makes the copy members of this class counter-intuitive. Instead of copying the count, the copy members instead do not copy anything at all! They simply 0 the lhs in the case of a construction, or in the case of an assignment, they just don't do anything at all.
After such a copy, the invariant is preserved. If instead the counts were actually copied, then after a copy, the number of deletions as reported by the sum of all deleters would have doubled.
The move members of the deleter, on the other hand, will transfer
the delete counts from the source to the target, zeroing out the source. Thus
the move members also preserve the invariant.
Because we want to be able to transfer counts from
deleter<derived> to deleter<base>,
converting constructors are set up. We set up both a converting copy constructor
and a converting move constructor. These constructors are constrained to allow
converting from deleter<derived> to
deleter<base> but not vice-versa. We also want to allow
converting assignments. In the interest of keeping this example as simple as
possible, the converting assignments are implicitly handled by the converting
constructors followed by the special member assignment operators.
For ease of both writing and of presentation, a using declaration is employed:
template <class T> using Ptr = std::unique_ptr<T, deleter<T>&>;
Note the use of the lvalue-reference type for the unique_ptr
deleter. As we are interested in accumulating and observing the state of
these deleters, we are going to collect the data in a couple of "master"
deleters and have all Ptr's refer to these masters. This
is precisely why reference-deleters were designed into
unique_ptr in the first place.
Now lets exercise this code:
int
main()
{
deleter<base> db;
deleter<derived> dd;
std::cout << "db = " << db << '\n';
std::cout << "dd = " << dd << '\n';
{
Ptr<derived> pd(new derived, dd);
pd.reset(new derived);
Ptr<base> pb(nullptr, db);
pb = std::move(pd); // The converting move assignment!
std::cout << "pb.get_deleter() = " << pb.get_deleter() << '\n';
std::cout << "pd.get_deleter() = " << pd.get_deleter() << '\n';
}
std::cout << "db = " << db << '\n';
std::cout << "dd = " << dd << '\n';
}
Assuming base and derived class types, this code
compiles today (C++11 and C++14), and portably outputs:
db = 0 dd = 0 pb.get_deleter() = 0 pd.get_deleter() = 1 db = 1 dd = 1
Explanation: The "master" deleters db and
dd are first constructed. There is one to be used with
unique_ptr<base> and one for
unique_ptr<derived>. They are both initialized with zero
counts as confirmed by the print statements. Next a scope is opened and a
Ptr<derived> is constructed with a new
derived and a reference to dd. At this point
pd owns the pointer and refers to dd, which still
has a count of 0. Next pd is reset with another
non-null derived*. This bumps the count in dd up
to 1. Then a Ptr<base> is constructed with
nullptr and a reference to db (whose count is still
zero).
Finally we get up to the operation in question: the converting move assignment
operator. This will of course transfer the pointer from pd to
pb, but it also executes:
get_deleter() = std::forward<deleter<derived>&>(u.get_deleter());
which is a fancy way of saying:
get_deleter() = u.get_deleter();
That is, the deleters are converting-copy-assigned, which is a no-op, and so
the subsequent print statements confirm that the deleter referred to by
pb still has a count of zero, and the deleter referred to by
pd still has a count of one. Next, scope is exited, causing the
destruction of both Ptrs. Only pb actually calls
its deleter and increments the destructor count. The program
ends with printing out the counts for both master deleters, each with a count
of one.
If desired, we could use this same code with by-value deleters:
template <class T> using Ptr = std::unique_ptr<T, deleter<T>>;
The result would be a little different, just as valid, but would not demonstrate the problem with the current proposed resolution to LWG 2228, and thus the use of the reference-deleters in this example.
Thanks to Geoffrey Romer for opening LWG 2228, and thus bringing attention to this very real defect.