strongdef: swap template params, move to dedicated namespace

This commit is contained in:
Danny Robson 2018-06-27 17:58:45 +10:00
parent eef3327415
commit 0df16bb9ee
3 changed files with 27 additions and 29 deletions

View File

@ -16,11 +16,9 @@
#include "strongdef.hpp"
using util::strongdef;
///////////////////////////////////////////////////////////////////////////////
// This instantiation is not meant to be exported, only being used as a local
// compilation error canary.
template struct util::strongdef<unsigned,void>;
template struct util::strongdef::index<void,unsigned>;

View File

@ -14,8 +14,7 @@
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_STRONGDEF_HPP
#define __UTIL_STRONGDEF_HPP
#pragma once
#include "cast.hpp"
#include "types/traits.hpp"
@ -25,25 +24,23 @@
#include <iosfwd>
namespace util {
namespace util::strongdef {
/// A transparent wrapper around a (typically lightweight) type for the
/// purposes of overload disambiguation. It acts like a typesafe typedef.
template <typename T,typename Tag>
struct strongdef {
public:
template <typename TagT, typename T = typename TagT::index_type>
struct index {
using value_type = T;
using tag_type = Tag;
using tag_type = TagT;
// TODO: ideally we'd default the constructor, but templated
// constructors can't be defaulted? So we just stub one out.
template <typename = std::enable_if_t<std::is_default_constructible_v<T>>>
constexpr explicit strongdef () { ; }
constexpr explicit index () { ; }
constexpr explicit strongdef (util::types::identity_t<T> const &_data): data (_data) { ; }
constexpr strongdef (strongdef const&) = default;
constexpr explicit index (util::types::identity_t<T> const &_data): data (_data) { ; }
constexpr index (index const&) = default;
strongdef& operator= (T const &) = delete;
strongdef& operator= (strongdef const &) = default;
index& operator= (T const &) = delete;
index& operator= (index const &) = default;
// conversion operators must not be explicit or it defeats the point
// of this class (ease of use, transparency).
@ -51,30 +48,32 @@ namespace util {
explicit operator T& (void) & { return data; }
constexpr auto operator== (T const &) = delete;
constexpr auto operator== (strongdef const &rhs) const
constexpr auto operator== (index const &rhs) const
{
return data == rhs.data;
}
constexpr auto operator!= (T const&) = delete;
constexpr auto operator!= (strongdef const &rhs) const
constexpr auto operator!= (index const &rhs) const
{
return data != rhs.data;
}
constexpr auto operator< (T const &) const = delete;
constexpr auto operator< (strongdef const &rhs) const { return data < rhs.data; }
constexpr auto operator< (index const &rhs) const { return data < rhs.data; }
constexpr auto operator> (T const &) const = delete;
constexpr auto operator> (strongdef const &rhs) const { return data > rhs.data; }
constexpr auto operator> (index const &rhs) const { return data > rhs.data; }
constexpr auto& operator++ (void)& { ++data; return *this; }
T data;
};
template <typename ValueT, typename TagT>
template <typename TagT, typename T>
std::ostream&
operator<< (std::ostream &os, strongdef<ValueT,TagT> const &val)
operator<< (std::ostream &os, index<TagT,T> const &val)
{
return os << val.data;
}
@ -131,10 +130,11 @@ static auto indices (ContainerT const &obj)
}
#if 0
namespace std {
template <typename T, typename Tag>
struct numeric_limits<util::strongdef<T,Tag>> {
using value_type = typename util::strongdef<T,Tag>::value_type;
template <typename TagT, typename T, template<typename> class ...OperationsT>
struct numeric_limits<util::strongdef::index<TagT,T,OperationsT...>> {
using value_type = util::strongdef::index<TagT,T,OperationsT...>;
static constexpr bool is_specialized = numeric_limits<value_type>::is_specialized;
static constexpr bool is_signed = numeric_limits<value_type>::is_signed;
@ -171,5 +171,4 @@ namespace std {
static constexpr value_type denorm_min (void) { return numeric_limits<value_type>::denorm_min (); }
};
}
#endif

View File

@ -1,23 +1,24 @@
#include "tap.hpp"
#include "strongdef.hpp"
using util::strongdef;
///////////////////////////////////////////////////////////////////////////////
int
main (void)
{
util::TAP::logger tap;
// These tests are less about functional testing, and more about link testing.
strongdef<unsigned,void> fortytwo (42u);
util::strongdef::index<void,unsigned> fortytwo (42u);
tap.expect_eq (fortytwo.data, 42u, "raw data equality");
tap.expect_eq (fortytwo, decltype(fortytwo) (42u), "passthrough equality");
#if 0
// 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");
#endif
return tap.status ();
}