Skip to content

🎨 Allow implicit conversion of format_result to ct_string #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/asciidoctor-ghpages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:

- name: Install Mermaid
run: |
sudo npm install -g @mermaid-js/mermaid-cli@11.2.1
npm install -g @mermaid-js/mermaid-cli@11.4.2
npx puppeteer browsers install chrome-headless-shell

- name: Install asciidoctor
Expand Down
3 changes: 3 additions & 0 deletions include/stdx/ct_format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ struct fmt::formatter<stdx::ct_string<N>> : fmt::formatter<std::string_view> {
namespace stdx {
inline namespace v1 {
template <typename Str, typename Args> struct format_result {
CONSTEVAL static auto
ct_string_convertible() -> std::bool_constant<Args::size() == 0>;

[[no_unique_address]] Str str;
[[no_unique_address]] Args args{};

Expand Down
22 changes: 22 additions & 0 deletions include/stdx/ct_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,29 @@
#if __cplusplus >= 202002L

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

#include <array>
#include <concepts>
#include <cstddef>
#include <string_view>
#include <type_traits>
#include <utility>

namespace stdx {
inline namespace v1 {

template <std::size_t N> struct ct_string;

namespace detail {
template <typename T>
concept format_convertible = requires(T t) {
{ T::ct_string_convertible() } -> std::same_as<std::true_type>;
{ ct_string{t.str.value} };
};
} // namespace detail

template <std::size_t N> struct ct_string {
CONSTEVAL ct_string() = default;

Expand All @@ -23,6 +37,10 @@ template <std::size_t N> struct ct_string {
}
}

template <detail::format_convertible T>
// NOLINTNEXTLINE(google-explicit-constructor)
CONSTEVAL explicit(false) ct_string(T t) : ct_string(t.str.value) {}

CONSTEVAL explicit(true) ct_string(char const *str, std::size_t sz) {
for (auto i = std::size_t{}; i < sz; ++i) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-*)
Expand Down Expand Up @@ -59,6 +77,9 @@ template <std::size_t N> struct ct_string {
std::array<char, N> value{};
};

template <detail::format_convertible T>
ct_string(T) -> ct_string<decltype(std::declval<T>().str.value)::capacity()>;

template <std::size_t N, std::size_t M>
[[nodiscard]] constexpr auto operator==(ct_string<N> const &lhs,
ct_string<M> const &rhs) -> bool {
Expand Down Expand Up @@ -116,6 +137,7 @@ operator+(ct_string<N> const &lhs,
}

template <ct_string S> struct cts_t {
using value_type = decltype(S);
constexpr static auto value = S;
};

Expand Down
17 changes: 17 additions & 0 deletions test/ct_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ TEST_CASE("format a format result", "[ct_format]") {
stdx::make_tuple(2022)});
}

namespace {
template <stdx::ct_string> constexpr auto conversion_success = true;
} // namespace

TEST_CASE("empty format_result can implicitly convert to ct_string",
"[ct_format]") {
using namespace std::string_view_literals;
static_assert(
stdx::detail::format_convertible<decltype(stdx::ct_format<"Hello">())>);
static_assert(stdx::detail::format_convertible<
decltype(stdx::ct_format<"Hello {}">("world"_ctst))>);
static_assert(not stdx::detail::format_convertible<
decltype(stdx::ct_format<"Hello {}">(42))>);

static_assert(conversion_success<stdx::ct_format<"Hello">()>);
}

namespace {
template <typename T, T...> struct string_constant {
private:
Expand Down