jeudi 15 décembre 2022

use case for constructors for gsl::not_null taking value type

gsl::not_null has 2 constructors one taking a forwarding reference and another one taking a value type. What is the case where the forwarding constructor alone would not work? Implementation from here

template <class T>
class not_null
{
public:
    static_assert(details::is_comparable_to_nullptr<T>::value, "T cannot be compared to nullptr.");

    template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
    constexpr not_null(U&& u) : ptr_(std::forward<U>(u))
    {
        Expects(ptr_ != nullptr);
    }

    template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
    constexpr not_null(T u) : ptr_(std::move(u))
    {
        Expects(ptr_ != nullptr);
    }
...

Aucun commentaire:

Enregistrer un commentaire