Skip to content

Commit dace94f

Browse files
authored
Merge pull request #250 from elbeno/cx-strings
🎨 Make `cts_t` and `format_result` easier to format
2 parents 3cb1a05 + c52ed4c commit dace94f

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

include/stdx/ct_format.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ template <typename Str, typename Args> struct format_result {
5151
format_result const &) -> bool = default;
5252
};
5353

54+
template <typename Str, typename Args>
55+
requires(Args::size() == 0 and is_cx_value_v<Str>)
56+
struct format_result<Str, Args> {
57+
CONSTEVAL static auto ct_string_convertible() -> std::true_type;
58+
59+
[[no_unique_address]] Str str;
60+
[[no_unique_address]] Args args{};
61+
62+
friend constexpr auto operator+(format_result const &fr) { return +fr.str; }
63+
64+
constexpr auto operator()() const noexcept { return +(*this); }
65+
using cx_value_t [[maybe_unused]] = void;
66+
67+
private:
68+
friend constexpr auto operator==(format_result const &,
69+
format_result const &) -> bool = default;
70+
};
71+
5472
template <typename Str, typename Args>
5573
format_result(Str, Args) -> format_result<Str, Args>;
5674
template <typename Str> format_result(Str) -> format_result<Str, tuple<>>;

include/stdx/ct_string.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ template <ct_string S> struct cts_t {
145145

146146
CONSTEVAL static auto ct_string_convertible() -> std::true_type;
147147
friend constexpr auto operator+(cts_t const &) { return value; }
148+
constexpr auto operator()() const noexcept { return value; }
149+
using cx_value_t [[maybe_unused]] = void;
148150
};
149151

150152
template <ct_string X, ct_string Y>

include/stdx/type_traits.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,5 @@ STDX_PRAGMA(diagnostic pop)
274274
template <typename T, typename = void> constexpr auto is_complete_v = false;
275275
template <typename T>
276276
constexpr auto is_complete_v<T, detail::void_v<sizeof(T)>> = true;
277-
278277
} // namespace v1
279278
} // namespace stdx

test/ct_format.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,21 @@ TEST_CASE("FORMAT a type argument", "[ct_format]") {
263263
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", int) == "Hello int"_fmt_res);
264264
}
265265

266-
TEST_CASE("FORMAT a constexpr string argument", "[ct_format]") {
266+
TEST_CASE("FORMAT a constexpr ct_string argument", "[ct_format]") {
267267
constexpr static auto S = "world"_cts;
268268
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", S) == "Hello world"_fmt_res);
269269
}
270270

271+
TEST_CASE("FORMAT a cts_t argument", "[ct_format]") {
272+
auto S = "world"_ctst;
273+
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", S) == "Hello world"_fmt_res);
274+
}
275+
276+
TEST_CASE("FORMAT a format_result argument", "[ct_format]") {
277+
auto S = "world"_fmt_res;
278+
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", S) == "Hello world"_fmt_res);
279+
}
280+
271281
TEST_CASE("FORMAT a constexpr int argument", "[ct_format]") {
272282
constexpr static auto I = 17;
273283
STATIC_REQUIRE(STDX_CT_FORMAT("Hello {}", I) == "Hello 17"_fmt_res);

0 commit comments

Comments
 (0)