Added explicit constructor for casting between related not-nullable pointers.

This commit is contained in:
Patrick 2025-07-12 12:49:45 +02:00
parent 018c75a5ed
commit 042e0d2465

View File

@ -35,6 +35,10 @@ concept nullable_type = !std::is_same_v<T, std::nullptr_t> && requires(T t) { t
template<nullable_type T>
class NotNullable
{
public:
using wrapped_t = T;
using element_type = std::remove_reference_t<decltype(*std::declval<const wrapped_t>())>;
using pointer = element_type*;
private:
T base_;
public:
@ -55,6 +59,12 @@ public:
{
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
}
template<typename TOther> requires(std::is_base_of_v<typename NotNullable<TOther>::element_type, element_type> && std::is_constructible_v<T, pointer>)
explicit constexpr NotNullable(const NotNullable<TOther>& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, pointer>))
: base_(static_cast<pointer>(&*other))
{
MIJIN_ASSERT(base_ != nullptr, "Constructed non-nullable type with nullptr.");
}
template<typename TOther> requires(std::is_constructible_v<T, TOther&&>)
constexpr NotNullable(NotNullable<TOther>&& other) MIJIN_NOEXCEPT_IF((std::is_nothrow_constructible_v<T, TOther&&>))
: base_(std::exchange(other.base_, nullptr))