Skip to content

not_null doesn't allow implicit conversion of a unique_pt #1022

@BenFrantzDale

Description

@BenFrantzDale

An rvalue not_null<unique_ptr<Child>> won't convert to a not_null<unique_ptr<Base>> like it does with a unique_ptr or a not_null<shared_ptr<>>: https://godbolt.org/z/o7nGEde3s

I'd expect the rvalue to move its unique_ptr out, leaving it in a moved-from state that, yes, is null, but like stlab::copy_on_write<T> it would be in a state that nobody should be reading from.

It won't allow conversion to raw unique_ptr<Base> either, like std::unique_ptr<Base> b = not_null(std::make_unique<Child>());.

Related: std::unique_ptr<int> p = not_null(std::make_unique<int>()); doesn't compile either. I think it needs

constexpr const T& get() const& { 
    Ensures(ptr_ != nullptr);
    return ptr_;
};
constexpr T&& get() && {
    Ensures(ptr_ != nullptr);
    return std::move(ptr_);
};
constexpr operator const T&() const& { return get(); }
constexpr operator const T&&() && { return std::move(*this).get(); }

although probably more to allow it to convert a not_null<unique_ptr<Child>>&& to unique_ptr<Base>.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions