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
5 changes: 5 additions & 0 deletions include/stdx/ct_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if __cplusplus >= 202002L

#include <stdx/compiler.hpp>
#include <stdx/utility.hpp>

#include <array>
#include <cstddef>
Expand Down Expand Up @@ -128,6 +129,10 @@ constexpr auto operator+(cts_t<X>, cts_t<Y>) {
return cts_t<X + Y>{};
}

namespace detail {
template <std::size_t N> struct ct_helper<ct_string<N>>;
} // namespace detail

template <ct_string Value> CONSTEVAL auto ct() { return cts_t<Value>{}; }

inline namespace literals {
Expand Down
17 changes: 15 additions & 2 deletions include/stdx/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,23 @@ constexpr auto is_aligned_with = [](auto v) -> bool {
}
};

template <auto Value> CONSTEVAL auto ct() {
return std::integral_constant<decltype(Value), Value>{};
#if __cplusplus >= 202002L

namespace detail {
template <typename T> struct ct_helper {
// NOLINTNEXTLINE(google-explicit-constructor)
CONSTEVAL ct_helper(T t) : value(t) {}
T value;
};
template <typename T> ct_helper(T) -> ct_helper<T>;
} // namespace detail

template <detail::ct_helper Value> CONSTEVAL auto ct() {
return std::integral_constant<decltype(Value.value), Value.value>{};
}
template <typename T> CONSTEVAL auto ct() { return type_identity<T>{}; }

#endif
} // namespace v1
} // namespace stdx

Expand Down
6 changes: 4 additions & 2 deletions test/ct_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ TEST_CASE("wrap ct_string in type", "[ct_string]") {

TEST_CASE("ct (ct_string)", "[ct_string]") {
using namespace stdx::ct_string_literals;
constexpr auto v = stdx::ct<"Hello">();
static_assert(v == "Hello"_ctst);
constexpr auto v1 = stdx::ct<"Hello">();
static_assert(v1 == "Hello"_ctst);
constexpr auto v2 = stdx::ct<"Hello"_cts>();
static_assert(v2 == "Hello"_ctst);
}
4 changes: 4 additions & 0 deletions test/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ TEST_CASE("is_aligned_with (pointer)", "[utility]") {
CHECK(stdx::is_aligned_with<std::uint32_t>(p));
}

#if __cplusplus >= 202002L

TEST_CASE("ct (integral)", "[utility]") {
constexpr auto vs = stdx::ct<42>();
static_assert(
Expand Down Expand Up @@ -239,3 +241,5 @@ TEST_CASE("ct (type)", "[utility]") {
constexpr auto v = stdx::ct<int>();
static_assert(std::is_same_v<decltype(v), stdx::type_identity<int> const>);
}

#endif