Add checked, explicit conversion from strongly typed number for size_t

This commit is contained in:
Maxime Coste 2016-04-22 20:48:42 +01:00
parent 21644d17d6
commit 717e37f2f3

View File

@ -1,6 +1,7 @@
#ifndef units_hh_INCLUDED #ifndef units_hh_INCLUDED
#define units_hh_INCLUDED #define units_hh_INCLUDED
#include "assert.hh"
#include "hash.hh" #include "hash.hh"
#include <type_traits> #include <type_traits>
@ -115,7 +116,9 @@ public:
friend size_t hash_value(RealType val) { return hash_value(val.m_value); } friend size_t hash_value(RealType val) { return hash_value(val.m_value); }
friend size_t abs(RealType val) { return val.m_value < ValueType(0) ? -val.m_value : val.m_value; } friend size_t abs(RealType val) { return val.m_value < ValueType(0) ? -val.m_value : val.m_value; }
private: explicit operator size_t() { kak_assert(m_value >= 0); return (size_t)m_value; }
protected:
ValueType m_value; ValueType m_value;
}; };