'Need help understanding this c++ template/enum
I am reading through the SafeInt.hpp code from Microsoft Docs (V 3.0.26) and stumbled upon the following code. I am not well versed in templates as used in an enum statement.
I am specifically bamboozled by "method = (....) ? xxxxx : "
Any help understanding it will be greatly appreciated. Thanks.
//core logic to determine casting behavior
enum CastMethod
{
CastOK = 0,
CastCheckLTZero,
CastCheckGTMax,
CastCheckSafeIntMinMaxUnsigned,
CastCheckSafeIntMinMaxSigned,
CastToFloat,
CastFromFloat,
CastToBool,
CastFromBool,
CastFromEnum
};
template < typename ToType, typename FromType >
class GetCastMethod
{
public:
enum
{
method = ( safeint_internal::numeric_type<FromType>::isEnum ) ? CastFromEnum :
( safeint_internal::int_traits< FromType >::isBool &&
!safeint_internal::int_traits< ToType >::isBool ) ? CastFromBool :
( !safeint_internal::int_traits< FromType >::isBool &&
safeint_internal::int_traits< ToType >::isBool ) ? CastToBool :
( safeint_internal::type_compare< ToType, FromType >::isCastOK ) ? CastOK :
( ( std::numeric_limits< ToType >::is_signed &&
!std::numeric_limits< FromType >::is_signed &&
sizeof( FromType ) >= sizeof( ToType ) ) ||
( safeint_internal::type_compare< ToType, FromType >::isBothUnsigned &&
sizeof( FromType ) > sizeof( ToType ) ) ) ? CastCheckGTMax :
( !std::numeric_limits< ToType >::is_signed &&
std::numeric_limits< FromType >::is_signed &&
sizeof( ToType ) >= sizeof( FromType ) ) ? CastCheckLTZero :
( !std::numeric_limits< ToType >::is_signed ) ? CastCheckSafeIntMinMaxUnsigned
: CastCheckSafeIntMinMaxSigned
};
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
