From 8499d0897560709dd6ba58591148407ad3530ad8 Mon Sep 17 00:00:00 2001 From: "Markus Kitsinger (SwooshyCueb)" Date: Tue, 4 Feb 2025 13:14:47 -0600 Subject: [PATCH] Move is_compiled_string to public API --- include/fmt/compile.h | 18 +++++++++--------- test/compile-test.cc | 12 ++++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/include/fmt/compile.h b/include/fmt/compile.h index 68b451c71d9c..3a341f23ada0 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -19,11 +19,11 @@ FMT_BEGIN_NAMESPACE // A compile-time string which is compiled into fast formatting code. FMT_EXPORT class compiled_string {}; -namespace detail { - template struct is_compiled_string : std::is_base_of {}; +namespace detail { + /** * Converts a string literal `s` into a format string that will be parsed at * compile time and converted into efficient formatting code. Requires C++17 @@ -425,7 +425,7 @@ constexpr auto compile_format_string(S fmt) { } template ::value)> + FMT_ENABLE_IF(is_compiled_string::value)> constexpr auto compile(S fmt) { constexpr auto str = basic_string_view(fmt); if constexpr (str.size() == 0) { @@ -461,7 +461,7 @@ constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf, } template ::value)> + FMT_ENABLE_IF(is_compiled_string::value)> FMT_INLINE std::basic_string format(const S&, Args&&... args) { if constexpr (std::is_same::value) { @@ -488,7 +488,7 @@ FMT_INLINE std::basic_string format(const S&, } template ::value)> + FMT_ENABLE_IF(is_compiled_string::value)> FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) { constexpr auto compiled = detail::compile(S()); if constexpr (std::is_same, @@ -503,7 +503,7 @@ FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) { #endif template ::value)> + FMT_ENABLE_IF(is_compiled_string::value)> auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args) -> format_to_n_result { using traits = detail::fixed_buffer_traits; @@ -513,7 +513,7 @@ auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args) } template ::value)> + FMT_ENABLE_IF(is_compiled_string::value)> FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args) -> size_t { auto buf = detail::counting_buffer<>(); @@ -522,7 +522,7 @@ FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args) } template ::value)> + FMT_ENABLE_IF(is_compiled_string::value)> void print(std::FILE* f, const S& fmt, const Args&... args) { auto buf = memory_buffer(); fmt::format_to(appender(buf), fmt, args...); @@ -530,7 +530,7 @@ void print(std::FILE* f, const S& fmt, const Args&... args) { } template ::value)> + FMT_ENABLE_IF(is_compiled_string::value)> void print(const S& fmt, const Args&... args) { print(stdout, fmt, args...); } diff --git a/test/compile-test.cc b/test/compile-test.cc index d708aa7882c4..88b8269b2a56 100644 --- a/test/compile-test.cc +++ b/test/compile-test.cc @@ -316,6 +316,18 @@ TEST(compile_test, compile_format_string_literal) { } #endif +#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) +template +bool check_is_compiled_string(const S&) { + return fmt::is_compiled_string::value; +} + +TEST(compile_test, is_compiled_string) { + EXPECT_TRUE(check_is_compiled_string(FMT_COMPILE("asdf"))); + EXPECT_TRUE(check_is_compiled_string(FMT_COMPILE("{}"))); +} +#endif + // MSVS 2019 19.29.30145.0 - OK // MSVS 2022 19.32.31332.0, 19.37.32826.1 - compile-test.cc(362,3): fatal error // C1001: Internal compiler error.