diff --git a/mfbt/NotNull.h b/mfbt/NotNull.h index 458d8d46a147c..6eed1ed395f4e 100644 --- a/mfbt/NotNull.h +++ b/mfbt/NotNull.h @@ -102,15 +102,15 @@ namespace mozilla { template class NotNull { - template friend NotNull WrapNotNull(U aBasePtr); + template friend constexpr NotNull WrapNotNull(U aBasePtr); template - friend NotNull MakeNotNull(Args&&... aArgs); + friend constexpr NotNull MakeNotNull(Args&&... aArgs); T mBasePtr; // This constructor is only used by WrapNotNull() and MakeNotNull(). template - explicit NotNull(U aBasePtr) : mBasePtr(aBasePtr) {} + constexpr explicit NotNull(U aBasePtr) : mBasePtr(aBasePtr) {} public: // Disallow default construction. @@ -118,7 +118,9 @@ class NotNull // Construct/assign from another NotNull with a compatible base pointer type. template - MOZ_IMPLICIT NotNull(const NotNull& aOther) : mBasePtr(aOther.get()) { + constexpr MOZ_IMPLICIT NotNull(const NotNull& aOther) + : mBasePtr(aOther.get()) + { static_assert(sizeof(T) == sizeof(NotNull), "NotNull must have zero space overhead."); static_assert(offsetof(NotNull, mBasePtr) == 0, @@ -136,18 +138,18 @@ class NotNull // Explicit conversion to a base pointer. Use only to resolve ambiguity or to // get a castable pointer. - const T& get() const { return mBasePtr; } + constexpr const T& get() const { return mBasePtr; } // Implicit conversion to a base pointer. Preferable to get(). - operator const T&() const { return get(); } + constexpr operator const T&() const { return get(); } // Dereference operators. - const T& operator->() const { return get(); } - decltype(*mBasePtr) operator*() const { return *mBasePtr; } + constexpr const T& operator->() const { return get(); } + constexpr decltype(*mBasePtr) operator*() const { return *mBasePtr; } }; template -NotNull +constexpr NotNull WrapNotNull(const T aBasePtr) { NotNull notNull(aBasePtr); @@ -192,7 +194,7 @@ struct PointedTo // |MakeNotNull>(args...)| will run |new Ob(args...)| // and return NotNull>. template -NotNull +constexpr NotNull MakeNotNull(Args&&... aArgs) { using Pointee = typename detail::PointedTo::NonConstType; @@ -203,13 +205,13 @@ MakeNotNull(Args&&... aArgs) // Compare two NotNulls. template -inline bool +constexpr bool operator==(const NotNull& aLhs, const NotNull& aRhs) { return aLhs.get() == aRhs.get(); } template -inline bool +constexpr bool operator!=(const NotNull& aLhs, const NotNull& aRhs) { return aLhs.get() != aRhs.get(); @@ -217,13 +219,13 @@ operator!=(const NotNull& aLhs, const NotNull& aRhs) // Compare a NotNull to a base pointer. template -inline bool +constexpr bool operator==(const NotNull& aLhs, const U& aRhs) { return aLhs.get() == aRhs; } template -inline bool +constexpr bool operator!=(const NotNull& aLhs, const U& aRhs) { return aLhs.get() != aRhs; @@ -231,13 +233,13 @@ operator!=(const NotNull& aLhs, const U& aRhs) // Compare a base pointer to a NotNull. template -inline bool +constexpr bool operator==(const T& aLhs, const NotNull& aRhs) { return aLhs == aRhs.get(); } template -inline bool +constexpr bool operator!=(const T& aLhs, const NotNull& aRhs) { return aLhs != aRhs.get();