Skip to content

Enum isn't sufficiently excluded #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions SafeInt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<U>::value, "Enum not allowed");
return SafeCastHelper< U, T, GetCastMethod< U, T >::method >::Cast( From, To );
}

Expand Down Expand Up @@ -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<T>::value, "Enum not allowed");
}

// Having a constructor for every type of int
Expand All @@ -5475,25 +5479,29 @@ 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<T>::value, "Enum not allowed");
//always safe
}

// provide explicit boolean converter
_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<T>::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<T>::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<T>::value, "Enum not allowed");
m_int = t.m_int;
}

Expand All @@ -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<T>::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 );
Expand Down