Here is a test file from gcc, live demo
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;
static_assert(!std::is_assignable<decltype(p2), decltype(p1)>::value, ""); // note ! here.
}
If the expression
std::declval<T>() = std::declval<U>()
is well-formed in unevaluated context, provides the member constant value equal true. Otherwise, value is false. Access checks are performed as if from a context unrelated to either type.
template<class T>
typename std::add_rvalue_reference<T>::type declval() noexcept;
The return type is
T&&
unlessT
is (possibly cv-qualified) void, in which case the return type is T.
Let's look at MoveAssignOnly
:
struct MoveAssignOnly {
MoveAssignOnly &operator=(MoveAssignOnly &) = delete;
MoveAssignOnly &operator=(MoveAssignOnly &&) = default;
};
int main()
{
static_assert(
not std::is_assignable<MoveAssignOnly, MoveAssignOnly>::value, "");
}
error: static_assert failed due to requirement '!std::is_assignable<MoveAssignOnly, MoveAssignOnly>::value'
Yes, it fails to compile because it provides a move assignment
Let's return to the gcc's test file and std::unique_ptr
. As we know, std::unique_ptr
also has move assignments.
However, unlike struct MoveAssignOnly
, static_assert(!std::is_assignable<decltype(p2), decltype(p1)>::value, "");
(more clearly, static_assert(!std::is_assignable<std::unique_ptr<int>, std::unique_ptr<int, do_nothing>>::value, "");
compiles happily.
I have struggled with libcxx's implementation of unique_ptr
for long time, but still cannot figure out: how can std::unique_ptr
be not assignable(! is_assignable
) when std::unique_ptr
provides move assignments?
std::unique_ptr
is assignable. – Lohrmemory
header. Note this SFINAE applied in template parameters:class = _EnableIfDeleterAssignable<_Ep>
. – Ipomoea