Skip to content

Commit 0281d9a

Browse files
authored
Merge pull request #188 from elbeno/implicit-convert-fmt-result-to-ct-string
🎨 Allow implicit conversion of `format_result` to `ct_string`
2 parents 2834680 + 7fc7b00 commit 0281d9a

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

.github/workflows/asciidoctor-ghpages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242

4343
- name: Install Mermaid
4444
run: |
45-
sudo npm install -g @mermaid-js/mermaid-cli@11.2.1
45+
npm install -g @mermaid-js/mermaid-cli@11.4.2
4646
npx puppeteer browsers install chrome-headless-shell
4747
4848
- name: Install asciidoctor

include/stdx/ct_format.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ struct fmt::formatter<stdx::ct_string<N>> : fmt::formatter<std::string_view> {
3131
namespace stdx {
3232
inline namespace v1 {
3333
template <typename Str, typename Args> struct format_result {
34+
CONSTEVAL static auto
35+
ct_string_convertible() -> std::bool_constant<Args::size() == 0>;
36+
3437
[[no_unique_address]] Str str;
3538
[[no_unique_address]] Args args{};
3639

include/stdx/ct_string.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@
33
#if __cplusplus >= 202002L
44

55
#include <stdx/compiler.hpp>
6+
#include <stdx/type_traits.hpp>
67
#include <stdx/utility.hpp>
78

89
#include <array>
10+
#include <concepts>
911
#include <cstddef>
1012
#include <string_view>
13+
#include <type_traits>
1114
#include <utility>
1215

1316
namespace stdx {
1417
inline namespace v1 {
18+
19+
template <std::size_t N> struct ct_string;
20+
21+
namespace detail {
22+
template <typename T>
23+
concept format_convertible = requires(T t) {
24+
{ T::ct_string_convertible() } -> std::same_as<std::true_type>;
25+
{ ct_string{t.str.value} };
26+
};
27+
} // namespace detail
28+
1529
template <std::size_t N> struct ct_string {
1630
CONSTEVAL ct_string() = default;
1731

@@ -23,6 +37,10 @@ template <std::size_t N> struct ct_string {
2337
}
2438
}
2539

40+
template <detail::format_convertible T>
41+
// NOLINTNEXTLINE(google-explicit-constructor)
42+
CONSTEVAL explicit(false) ct_string(T t) : ct_string(t.str.value) {}
43+
2644
CONSTEVAL explicit(true) ct_string(char const *str, std::size_t sz) {
2745
for (auto i = std::size_t{}; i < sz; ++i) {
2846
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-*)
@@ -59,6 +77,9 @@ template <std::size_t N> struct ct_string {
5977
std::array<char, N> value{};
6078
};
6179

80+
template <detail::format_convertible T>
81+
ct_string(T) -> ct_string<decltype(std::declval<T>().str.value)::capacity()>;
82+
6283
template <std::size_t N, std::size_t M>
6384
[[nodiscard]] constexpr auto operator==(ct_string<N> const &lhs,
6485
ct_string<M> const &rhs) -> bool {
@@ -116,6 +137,7 @@ operator+(ct_string<N> const &lhs,
116137
}
117138

118139
template <ct_string S> struct cts_t {
140+
using value_type = decltype(S);
119141
constexpr static auto value = S;
120142
};
121143

test/ct_format.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ TEST_CASE("format a format result", "[ct_format]") {
142142
stdx::make_tuple(2022)});
143143
}
144144

145+
namespace {
146+
template <stdx::ct_string> constexpr auto conversion_success = true;
147+
} // namespace
148+
149+
TEST_CASE("empty format_result can implicitly convert to ct_string",
150+
"[ct_format]") {
151+
using namespace std::string_view_literals;
152+
static_assert(
153+
stdx::detail::format_convertible<decltype(stdx::ct_format<"Hello">())>);
154+
static_assert(stdx::detail::format_convertible<
155+
decltype(stdx::ct_format<"Hello {}">("world"_ctst))>);
156+
static_assert(not stdx::detail::format_convertible<
157+
decltype(stdx::ct_format<"Hello {}">(42))>);
158+
159+
static_assert(conversion_success<stdx::ct_format<"Hello">()>);
160+
}
161+
145162
namespace {
146163
template <typename T, T...> struct string_constant {
147164
private:

0 commit comments

Comments
 (0)