Skip to content

Commit 768a457

Browse files
authored
Merge pull request #220 from elbeno/user-defined-formatting
✨ Allow user-defined formatting with `ct_format`
2 parents ac1a4e5 + ab9b70d commit 768a457

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

include/stdx/ct_format.hpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ template <ct_string Fmt> struct fmt_data {
205205
constexpr static auto splits = split_specifiers<N + 1>(fmt);
206206
constexpr static auto last_cts = to_ct_string<splits[N].size()>(splits[N]);
207207
};
208+
209+
template <typename T>
210+
constexpr auto ct_format_as(T const &t) -> decltype(auto) {
211+
return (t);
212+
}
208213
} // namespace detail
209214

210215
template <ct_string Fmt,
@@ -223,7 +228,8 @@ constexpr auto ct_format = [](auto &&...args) {
223228
};
224229

225230
auto const result = [&]<std::size_t... Is>(std::index_sequence<Is...>) {
226-
return (format1.template operator()<Is>(FWD(args)) + ... +
231+
using detail::ct_format_as;
232+
return (format1.template operator()<Is>(ct_format_as(FWD(args))) + ... +
227233
format_result{cts_t<data::last_cts>{}});
228234
}(std::make_index_sequence<data::N>{});
229235
return format_result{detail::convert_output<result.str.value, Output>(),

test/ct_format.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,18 @@ TEST_CASE("num fmt specifiers", "[ct_format]") {
223223
static_assert(stdx::num_fmt_specifiers<"{}"> == 1u);
224224
static_assert(stdx::num_fmt_specifiers<"{} {}"> == 2u);
225225
}
226+
227+
namespace user {
228+
struct S {
229+
int x;
230+
};
231+
232+
constexpr auto ct_format_as(S const &s) {
233+
return stdx::ct_format<"S: {}">(s.x);
234+
}
235+
} // namespace user
236+
237+
TEST_CASE("user-defined formatting", "[ct_format]") {
238+
auto r = stdx::ct_format<"Hello {}">(user::S{42});
239+
CHECK(r == stdx::format_result{"Hello S: {}"_ctst, stdx::make_tuple(42)});
240+
}

0 commit comments

Comments
 (0)