strongdef: add specialisation of numeric_limits

This commit is contained in:
Danny Robson 2015-11-18 14:43:13 +11:00
parent 4599b11197
commit 38678cc78e
2 changed files with 55 additions and 11 deletions

View File

@ -17,6 +17,8 @@
#ifndef __UTIL_STRONGDEF_HPP
#define __UTIL_STRONGDEF_HPP
#include <limits>
namespace util {
/// A transparent wrapper around a (typically lightweight) type for the
/// purposes of overload disambiguation. It acts like a typesafe typedef.
@ -26,24 +28,61 @@ namespace util {
using value_type = T;
constexpr strongdef () = default;
constexpr explicit strongdef (T _data):
data (_data)
{ ; }
constexpr explicit strongdef (const T &_data): data (_data) { ; }
constexpr strongdef (const strongdef &rhs): data (rhs.data) { ; }
strongdef& operator= (const strongdef &rhs)
{ data = rhs.data; return *this; }
strongdef& operator= (const T &rhs)
{ data = rhs; return *this; }
strongdef& operator= (const strongdef &rhs) { data = rhs.data; return *this; }
strongdef& operator= (const T &rhs) { data = rhs; return *this; }
operator const T& (void) const { return data; }
operator T& (void) { return data; }
bool operator== (const strongdef &rhs) const
{ return data == rhs.data; }
bool operator== (const strongdef &rhs) const { return data == rhs.data; }
T data;
};
}
namespace std {
template <typename T>
struct numeric_limits<util::strongdef<T>> {
using value_type = typename util::strongdef<T>::value_type;
static constexpr bool is_specialized = numeric_limits<value_type>::is_specialized;
static constexpr bool is_signed = numeric_limits<value_type>::is_signed;
static constexpr bool is_integer = numeric_limits<value_type>::is_integer;
static constexpr bool is_exact = numeric_limits<value_type>::is_exact;
static constexpr bool has_infinity = numeric_limits<value_type>::has_infinity;
static constexpr bool has_quiet_NaN = numeric_limits<value_type>::has_quiet_NaN;
static constexpr bool has_signaling_NaN = numeric_limits<value_type>::has_signaling_NaN;
static constexpr bool has_denorm = numeric_limits<value_type>::has_denorm;
static constexpr bool has_denorm_loss = numeric_limits<value_type>::has_denorm_loss;
static constexpr std::float_round_style round_style = numeric_limits<value_type>::round_style;
static constexpr bool is_iec559 = numeric_limits<value_type>::is_iec559;
static constexpr bool is_bounded = numeric_limits<value_type>::is_bounded;
static constexpr bool is_modulo = numeric_limits<value_type>::is_modulo;
static constexpr int digits = numeric_limits<value_type>::digits;
static constexpr int digits10 = numeric_limits<value_type>::digits10;
static constexpr int max_digits10 = numeric_limits<value_type>::max_digits10;
static constexpr int radix = numeric_limits<value_type>::radix;
static constexpr int min_exponent = numeric_limits<value_type>::min_exponent;
static constexpr int min_exponent10 = numeric_limits<value_type>::min_exponent10;
static constexpr int max_exponent = numeric_limits<value_type>::max_exponent;
static constexpr int max_exponent10 = numeric_limits<value_type>::max_exponent10;
static constexpr bool traps = numeric_limits<value_type>::traps;
static constexpr bool tinyness_before = numeric_limits<value_type>::tinyness_before;
static constexpr value_type min (void) { return numeric_limits<value_type>::min (); }
static constexpr value_type lowest (void) { return numeric_limits<value_type>::lowest (); }
static constexpr value_type max (void) { return numeric_limits<value_type>::max (); }
static constexpr value_type epsilon (void) { return numeric_limits<value_type>::epsilon (); }
static constexpr value_type round_error (void) { return numeric_limits<value_type>::round_error (); }
static constexpr value_type infinity (void) { return numeric_limits<value_type>::infinity (); }
static constexpr value_type quiet_NaN (void) { return numeric_limits<value_type>::quiet_NaN (); }
static constexpr value_type signaling_NaN (void) { return numeric_limits<value_type>::signaling_NaN (); }
static constexpr value_type denorm_min (void) { return numeric_limits<value_type>::denorm_min (); }
};
}
#endif

View File

@ -8,11 +8,16 @@ int
main (void)
{
util::TAP::logger tap;
// These tests are less about functional testing, and more about link testing.
strongdef<unsigned> fortytwo (42u);
tap.expect_eq (fortytwo.data, 42u, "raw data equality");
tap.expect_eq (fortytwo, 42u, "passthrough equality");
// Ensure numeric_limits has been specialised. Unknown types are default initialised, so check if we get non-zero for maximum.
tap.expect_eq (std::numeric_limits<decltype(fortytwo)>::max (),
std::numeric_limits<decltype(fortytwo)::value_type>::max (),
"numeric_limits has been specialised");
return tap.status ();
}