Skip to content

Commit 543e357

Browse files
authored
Merge pull request #244 from elbeno/ctst-implicit-convert
2 parents effadd4 + 1ce9e1b commit 543e357

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

include/stdx/ct_format.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ template <typename Str, typename Args> struct format_result {
3535
friend constexpr auto operator+(format_result const &fr)
3636
requires(decltype(ct_string_convertible())::value)
3737
{
38-
return ct_string{fr.str.value};
38+
return +fr.str;
3939
}
4040

4141
friend constexpr auto operator+(format_result const &) {

include/stdx/ct_string.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace detail {
2222
template <typename T>
2323
concept format_convertible = requires(T t) {
2424
{ T::ct_string_convertible() } -> std::same_as<std::true_type>;
25-
{ ct_string{t.str.value} };
25+
{ ct_string{+t} };
2626
};
2727
} // namespace detail
2828

@@ -39,7 +39,7 @@ template <std::size_t N> struct ct_string {
3939

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

4444
CONSTEVAL explicit(true) ct_string(char const *str, std::size_t sz) {
4545
for (auto i = std::size_t{}; i < sz; ++i) {
@@ -78,7 +78,7 @@ template <std::size_t N> struct ct_string {
7878
};
7979

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

8383
template <std::size_t N, std::size_t M>
8484
[[nodiscard]] constexpr auto operator==(ct_string<N> const &lhs,
@@ -142,6 +142,9 @@ template <std::size_t N, std::size_t M>
142142
template <ct_string S> struct cts_t {
143143
using value_type = decltype(S);
144144
constexpr static auto value = S;
145+
146+
CONSTEVAL static auto ct_string_convertible() -> std::true_type;
147+
friend constexpr auto operator+(cts_t const &) { return value; }
145148
};
146149

147150
template <ct_string X, ct_string Y>

test/ct_string.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,12 @@ TEST_CASE("ct (ct_string)", "[ct_string]") {
150150
constexpr auto v2 = stdx::ct<"Hello"_cts>();
151151
STATIC_REQUIRE(v2 == "Hello"_ctst);
152152
}
153+
154+
namespace {
155+
template <stdx::ct_string> constexpr auto conversion_success = true;
156+
} // namespace
157+
158+
TEST_CASE("cts_t can implicitly convert to ct_string", "[ct_string]") {
159+
using namespace stdx::ct_string_literals;
160+
STATIC_REQUIRE(conversion_success<"Hello"_ctst>);
161+
}

0 commit comments

Comments
 (0)