FTL: Allow implicit conversions with NonNull<T>
C++ allows certain implicit conversions for pointers, such as the
implicit conversion of "T*" to "const T*".
NonNull did not allow them, but that is easily corrected.
Bug: 185536303
Flag: EXEMPT Relaxes a compile-time constraint
Test: atest ftl_test
Change-Id: I0c14805c8fdcca979c67d2c86d57e5e678aa1b32
diff --git a/include/ftl/non_null.h b/include/ftl/non_null.h
index 4a5d8bf..e684288 100644
--- a/include/ftl/non_null.h
+++ b/include/ftl/non_null.h
@@ -68,6 +68,15 @@
constexpr NonNull(const NonNull&) = default;
constexpr NonNull& operator=(const NonNull&) = default;
+ template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, Pointer>>>
+ constexpr NonNull(const NonNull<U>& other) : pointer_(other.get()) {}
+
+ template <typename U, typename = std::enable_if_t<std::is_convertible_v<U, Pointer>>>
+ constexpr NonNull& operator=(const NonNull<U>& other) {
+ pointer_ = other.get();
+ return *this;
+ }
+
[[nodiscard]] constexpr const Pointer& get() const { return pointer_; }
[[nodiscard]] constexpr explicit operator const Pointer&() const { return get(); }