Skip to content

Commit 5196d26

Browse files
authored
Merge pull request #134 from elbeno/snippets
✨ Add `cts_t`, `shrink_t` and `expand_t`
2 parents 2fc35c7 + 3f611e2 commit 5196d26

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

include/stdx/ct_string.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ template <std::size_t N> struct ct_string {
5858
std::array<char, N> value{};
5959
};
6060

61+
template <stdx::ct_string S> struct cts_t {
62+
constexpr static auto value = S;
63+
};
64+
6165
template <std::size_t N, std::size_t M>
6266
[[nodiscard]] constexpr auto operator==(ct_string<N> const &lhs,
6367
ct_string<M> const &rhs) -> bool {

include/stdx/type_traits.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,12 @@ constexpr bool is_structural_v = detail::detect_structural<T>;
216216
template <typename T, typename = void> constexpr auto is_cx_value_v = false;
217217
template <typename T>
218218
constexpr auto is_cx_value_v<T, std::void_t<typename T::cx_value_t>> = true;
219+
220+
#if __cplusplus >= 202002L
221+
template <typename T>
222+
using shrink_t = decltype([]() -> T (*)() { return nullptr; });
223+
224+
template <typename T> using expand_t = decltype(T{}()());
225+
#endif
219226
} // namespace v1
220227
} // namespace stdx

test/ct_string.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,9 @@ TEST_CASE("template argument as CX_VALUE", "[ct_string]") {
130130
constexpr auto s = to_cx_value<"Hello">();
131131
static_assert(s() == "Hello"_cts);
132132
}
133+
134+
TEST_CASE("wrap ct_string in type", "[ct_string]") {
135+
using namespace stdx::ct_string_literals;
136+
using S = stdx::cts_t<"Hello">;
137+
static_assert(S::value == "Hello"_cts);
138+
}

test/type_traits.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <stdx/ct_conversions.hpp>
12
#include <stdx/type_traits.hpp>
23

34
#include <catch2/catch_test_macros.hpp>
@@ -237,3 +238,19 @@ struct S {
237238
TEST_CASE("non-structural types", "[type_traits]") {
238239
static_assert(not stdx::is_structural_v<non_structural::S>);
239240
}
241+
242+
#if __cplusplus >= 202002L
243+
namespace {
244+
template <typename...> struct long_type_name {};
245+
} // namespace
246+
247+
TEST_CASE("type shrinkage", "[type_traits]") {
248+
using A = long_type_name<int, int, int, int, int, int, int, int>;
249+
using B = long_type_name<A, A, A, A, A, A, A, A>;
250+
using C = long_type_name<B, B, B, B, B, B, B, B>;
251+
using X = stdx::shrink_t<C>;
252+
static_assert(stdx::type_as_string<X>().size() <
253+
stdx::type_as_string<C>().size());
254+
static_assert(std::same_as<stdx::expand_t<X>, C>);
255+
}
256+
#endif

0 commit comments

Comments
 (0)