Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions include/stdx/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,71 @@ CONSTEVAL auto shrink() {
}

template <typename T>
concept is_shrunk = requires(T t) {
concept is_shrunk = requires(T const &t) {
{ t()() } -> is_shrinkwrapped;
};

template <typename T> CONSTEVAL auto maybe_expand() -> T;
template <is_shrunk T>
CONSTEVAL auto maybe_expand() -> typename decltype(T{}()())::type;

template <typename T> constexpr auto maybe_expand(T &&t) -> decltype(auto) {
return T(std::forward<T>(t));
}
template <is_shrunk T>
constexpr auto maybe_expand(T &&) ->
typename decltype(std::remove_cvref_t<T>{}()())::type {
using R = typename decltype(std::remove_cvref_t<T>{}()())::type;
static_assert(std::is_default_constructible_v<R>,
"expand(T) cannot default-construct the return value: maybe "
"use expand_t<T> instead?");
static_assert(
std::is_empty_v<R>,
"Danger! expand(T) would return a default-constructed but non-empty "
"object: use expand_t<T>{} instead if this is what you want");
return R{};
}
} // namespace detail

template <typename T> CONSTEVAL auto shrink() -> decltype(detail::shrink<T>());
template <typename T> CONSTEVAL auto shrink() -> decltype(detail::shrink<T>()) {
static_assert(
always_false_v<T>,
"shrink<T>() should not be called outside an unevaluated context");
}
template <typename T>
CONSTEVAL auto shrink(T const &) -> decltype(detail::shrink<T>()) {
return {};
}

template <typename T>
CONSTEVAL auto expand() -> decltype(detail::maybe_expand<T>());
CONSTEVAL auto expand() -> decltype(detail::maybe_expand<T>()) {
using R = decltype(detail::maybe_expand<T>());
static_assert(
always_false_v<R>,
"expand<T>() should not be called outside an unevaluated context");
}
template <typename T> constexpr auto expand(T &&t) -> decltype(auto) {
return detail::maybe_expand(std::forward<T>(t));
}

#else
template <typename T> CONSTEVAL auto shrink() -> T;
template <typename T> CONSTEVAL auto expand() -> T;
template <typename T> CONSTEVAL auto shrink() -> T {
static_assert(
always_false_v<T>,
"shrink<T>() should not be called outside an unevaluated context");
}
template <typename T> constexpr auto shrink(T &&t) -> decltype(auto) {
return T(std::forward<T>(t));
}

template <typename T> CONSTEVAL auto expand() -> T {
static_assert(
always_false_v<T>,
"expand<T>() should not be called outside an unevaluated context");
}
template <typename T> constexpr auto expand(T &&t) -> decltype(auto) {
return T(std::forward<T>(t));
}
#endif

template <typename T> using shrink_t = decltype(shrink<T>());
Expand Down
23 changes: 17 additions & 6 deletions test/type_traits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,32 @@ TEST_CASE("non-structural types", "[type_traits]") {
STATIC_REQUIRE(not stdx::is_structural_v<non_structural::S>);
}

#if __cplusplus >= 202002L
namespace {
template <typename...> struct long_type_name {};
using A = long_type_name<int, int, int, int, int, int, int, int>;
using B = long_type_name<A, A, A, A, A, A, A, A>;
using C = long_type_name<B, B, B, B, B, B, B, B>;
} // namespace

TEST_CASE("type shrinkage", "[type_traits]") {
using A = long_type_name<int, int, int, int, int, int, int, int>;
using B = long_type_name<A, A, A, A, A, A, A, A>;
using C = long_type_name<B, B, B, B, B, B, B, B>;
TEST_CASE("type shrinkage (by type)", "[type_traits]") {
using X = stdx::shrink_t<C>;
#if __cplusplus >= 202002L
STATIC_CHECK(stdx::type_as_string<X>().size() <
stdx::type_as_string<C>().size());
STATIC_CHECK(std::same_as<stdx::expand_t<X>, C>);
#endif
STATIC_CHECK(std::is_same_v<stdx::expand_t<X>, C>);
}

TEST_CASE("type shrinkage (by value)", "[type_traits]") {
auto c = C{};
auto x = stdx::shrink(c);
auto y = stdx::expand(x);
#if __cplusplus >= 202002L
STATIC_CHECK(stdx::type_as_string<decltype(x)>().size() <
stdx::type_as_string<C>().size());
#endif
STATIC_CHECK(std::is_same_v<decltype(y), C>);
}

TEST_CASE("nth type in pack", "[type_traits]") {
STATIC_REQUIRE(
Expand Down