Skip to content

Commit

Permalink
Fix compatibility for gcc 4.8
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed Jun 22, 2022
1 parent 89a16a4 commit 84584e2
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions include/libcopp/utils/gsl/not_null.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,31 @@ using owner = T;
//
template <class T>
class not_null {
public:
using value_type = typename std::conditional<std::is_copy_constructible<T>::value, T, const T&>::type;

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

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

template <class = typename std::enable_if<!std::is_same<std::nullptr_t, T>::value>::type>
constexpr not_null(T u) : ptr_(std::move(u)) {
assert(ptr_ != nullptr);
}
constexpr not_null(T u) : ptr_(std::move(u)) {}

template <class U, class = typename std::enable_if<std::is_convertible<U, T>::value>::type>
constexpr not_null(const not_null<U>& other) : not_null(other.get()) {}

not_null(const not_null& other) = default;
not_null& operator=(const not_null& other) = default;
constexpr typename std::conditional<std::is_copy_constructible<T>::value, T, const T&>::type get() const {
constexpr value_type get() const {
assert(ptr_ != nullptr);
return ptr_;
}

constexpr operator T() const { return get(); }
constexpr decltype(auto) operator->() const { return get(); }
constexpr decltype(auto) operator*() const { return *get(); }
constexpr value_type operator->() const { return get(); }
constexpr typename std::remove_pointer<value_type>::type operator*() const { return *get(); }

// prevents compilation when someone attempts to assign a null pointer constant
not_null(std::nullptr_t) = delete;
Expand Down

0 comments on commit 84584e2

Please sign in to comment.