mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Add assignment from related types
This commit is contained in:
parent
62226021a8
commit
ab825037d0
@ -129,6 +129,12 @@ public:
|
||||
not_null<T>& operator=(std::nullptr_t) = delete;
|
||||
not_null<T>& operator=(int) = delete;
|
||||
|
||||
template <typename U, typename Dummy = std::enable_if_t<std::is_convertible<U, T>::value>>
|
||||
not_null<T> & operator=( not_null<U> const & other )
|
||||
{
|
||||
ptr_ = other.get(); ensure_invariant(); return *this;
|
||||
}
|
||||
|
||||
T get() const {
|
||||
#ifdef _MSC_VER
|
||||
__assume(ptr_ != nullptr);
|
||||
|
@ -86,6 +86,26 @@ SUITE(NotNullTests)
|
||||
|
||||
int* q = nullptr;
|
||||
CHECK_THROW(p = q, fail_fast);
|
||||
|
||||
// Allows assignment from a not_null related pointer type.
|
||||
{
|
||||
MyDerived derived;
|
||||
not_null<MyDerived*> p = &derived;
|
||||
not_null<MyBase*> q = p;
|
||||
|
||||
q = p;
|
||||
|
||||
CHECK(q == p);
|
||||
}
|
||||
|
||||
// Terminates assignment from related pointer types for null pointer value.
|
||||
{
|
||||
MyDerived* z = nullptr;
|
||||
MyDerived derived;
|
||||
not_null<MyBase*> p = &derived;
|
||||
|
||||
CHECK_THROW(p = z, fail_fast);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user