diff options
-rw-r--r-- | include/o3tl/strong_int.hxx | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/include/o3tl/strong_int.hxx b/include/o3tl/strong_int.hxx index ff2ba9123627..ce5466e45167 100644 --- a/include/o3tl/strong_int.hxx +++ b/include/o3tl/strong_int.hxx @@ -21,6 +21,8 @@ #define INCLUDED_O3TL_STRONG_INT_HXX #include <sal/config.h> +#include <limits> +#include <cassert> namespace o3tl { @@ -40,7 +42,15 @@ template <typename UNDERLYING_TYPE, typename PHANTOM_TYPE> struct strong_int { public: - explicit constexpr strong_int(UNDERLYING_TYPE value) : m_value(value) {} + explicit constexpr strong_int(long value) : m_value(value) + { +#if HAVE_CXX14_CONSTEXPR + // catch attempts to pass in out-of-range values early + assert(value >= std::numeric_limits<UNDERLYING_TYPE>::min() + && value <= std::numeric_limits<UNDERLYING_TYPE>::max() + && "out of range"); +#endif + } strong_int() : m_value(0) {} explicit constexpr operator UNDERLYING_TYPE() const { return m_value; } |