From f299082de8e487387b06f97f3b83297bf02608be Mon Sep 17 00:00:00 2001 From: David LeBlanc Date: Fri, 9 Aug 2024 11:03:11 -0700 Subject: [PATCH] Enum isn't sufficiently excluded --- SafeInt.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/SafeInt.hpp b/SafeInt.hpp index b5124ee..117df2c 100644 --- a/SafeInt.hpp +++ b/SafeInt.hpp @@ -5382,6 +5382,8 @@ template < typename T, typename U > class BinaryXorHelper< T, U, BinaryState_Int template < typename T, typename U > SAFE_INT_NODISCARD _CONSTEXPR11 inline bool SafeCast( const T From, U& To ) SAFEINT_NOTHROW { + // Cast from enum is allowed, cast to enum is not. + static_assert(!std::is_enum::value, "Enum not allowed"); return SafeCastHelper< U, T, GetCastMethod< U, T >::method >::Cast( From, To ); } @@ -5467,6 +5469,8 @@ template < typename T, typename E = SafeIntDefaultExceptionHandler > class SafeI _CONSTEXPR11 SafeInt() SAFEINT_NOTHROW : m_int(0) { static_assert( safeint_internal::numeric_type< T >::isInt, "Integer type required" ); + // This wasn't compiling before, but make it explicit + static_assert(!std::is_enum::value, "Enum not allowed"); } // Having a constructor for every type of int @@ -5475,6 +5479,7 @@ template < typename T, typename E = SafeIntDefaultExceptionHandler > class SafeI _CONSTEXPR11 SafeInt( const T& i ) SAFEINT_NOTHROW : m_int(i) { static_assert(safeint_internal::numeric_type< T >::isInt, "Integer type required"); + static_assert(!std::is_enum::value, "Enum not allowed"); //always safe } @@ -5482,18 +5487,21 @@ template < typename T, typename E = SafeIntDefaultExceptionHandler > class SafeI _CONSTEXPR11 SafeInt( bool b ) SAFEINT_NOTHROW : m_int((T)(b ? 1 : 0)) { static_assert(safeint_internal::numeric_type< T >::isInt, "Integer type required"); + static_assert(!std::is_enum::value, "Enum not allowed"); } template < typename U > _CONSTEXPR14 SafeInt(const SafeInt< U, E >& u) SAFEINT_CPP_THROW : m_int(0) { static_assert(safeint_internal::numeric_type< T >::isInt, "Integer type required"); + static_assert(!std::is_enum::value, "Enum not allowed"); m_int = (T)SafeInt< T, E >( (U)u ); } _CONSTEXPR14 SafeInt(const SafeInt< T, E >& t) SAFEINT_CPP_THROW : m_int(0) { static_assert(safeint_internal::numeric_type< T >::isInt, "Integer type required"); + static_assert(!std::is_enum::value, "Enum not allowed"); m_int = t.m_int; } @@ -5502,6 +5510,7 @@ template < typename T, typename E = SafeIntDefaultExceptionHandler > class SafeI { // m_int must be initialized to something to work with constexpr, because if it throws, then m_int is unknown static_assert(safeint_internal::numeric_type< T >::isInt, "Integer type required"); + static_assert(!std::is_enum::value, "Enum not allowed"); // SafeCast will throw exceptions if i won't fit in type T SafeCastHelper< T, U, GetCastMethod< T, U >::method >::template CastThrow< E >( i, m_int );