Skip to content

Commit

Permalink
Move is_strong_typedef to type_safe namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
BrukerJWD authored and foonathan committed May 2, 2022
1 parent 1c0d3a6 commit c0afc2d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
39 changes: 21 additions & 18 deletions include/type_safe/strong_typedef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ namespace detail
{
template <class Tag, typename T>
T underlying_type(strong_typedef<Tag, T>);

template <typename... Ts>
struct make_void
{
typedef void type;
};
template <typename... Ts>
using void_t = typename make_void<Ts...>::type;
} // namespace detail

/// The underlying type of the [ts::strong_typedef]().
Expand All @@ -113,6 +121,19 @@ template <class StrongTypedef>
using underlying_type
= decltype(detail::underlying_type(std::declval<typename std::decay<StrongTypedef>::type>()));


/// \group is_strong_typedef
/// Whether a type `T` is a [ts::strong_type]()
template <class T, typename = detail::void_t<>>
struct TYPE_SAFE_MSC_EMPTY_BASES is_strong_typedef : std::false_type
{};

/// \group is_strong_typedef
template <class T>
struct is_strong_typedef<T, detail::void_t<decltype(detail::underlying_type(std::declval<T>()))>>
: std::true_type
{};

/// Accesses the underlying value.
/// \returns A reference to the underlying value.
/// \group strong_typedef_get
Expand Down Expand Up @@ -195,24 +216,6 @@ namespace strong_typedef_op
return static_cast<T&&>(t);
}

template <typename... Ts>
struct make_void
{
typedef void type;
};
template <typename... Ts>
using void_t = typename make_void<Ts...>::type;

template <class T, typename = void_t<>>
struct TYPE_SAFE_MSC_EMPTY_BASES is_strong_typedef : std::false_type
{};

template <class T>
struct is_strong_typedef<
T, void_t<decltype(type_safe::detail::underlying_type(std::declval<T>()))>>
: std::true_type
{};

template <class StrongTypedef,
typename = typename std::enable_if<is_strong_typedef<StrongTypedef>::value>::type>
constexpr auto forward_or_underlying(StrongTypedef&& type) noexcept
Expand Down
12 changes: 11 additions & 1 deletion test/strong_typedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using namespace type_safe;
{}; \
template <typename Arg1, typename Arg2> \
struct CheckerName<Arg1, Arg2, \
strong_typedef_op::detail::void_t<decltype(static_cast<Arg1>( \
detail::void_t<decltype(static_cast<Arg1>( \
std::declval<Arg1>()) Op static_cast<Arg2>(std::declval<Arg2>()))>> \
: std::true_type \
{};
Expand Down Expand Up @@ -604,4 +604,14 @@ TEST_CASE("strong_typedef")
type b(foo{true});
REQUIRE(b);
}
SECTION("is_strong_typedef")
{
struct strong : strong_typedef<strong, int>
{
using strong_typedef::strong_typedef;
};

REQUIRE(is_strong_typedef<strong>::value);
REQUIRE(!is_strong_typedef<int>::value);
}
}

0 comments on commit c0afc2d

Please sign in to comment.