Skip to content

Commit f2cec91

Browse files
authored
Move is_compiled_string to public API (#4342)
1 parent d5b866e commit f2cec91

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

include/fmt/compile.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ FMT_BEGIN_NAMESPACE
1919
// A compile-time string which is compiled into fast formatting code.
2020
FMT_EXPORT class compiled_string {};
2121

22-
namespace detail {
23-
2422
template <typename S>
2523
struct is_compiled_string : std::is_base_of<compiled_string, S> {};
2624

25+
namespace detail {
26+
2727
/**
2828
* Converts a string literal `s` into a format string that will be parsed at
2929
* compile time and converted into efficient formatting code. Requires C++17
@@ -425,7 +425,7 @@ constexpr auto compile_format_string(S fmt) {
425425
}
426426

427427
template <typename... Args, typename S,
428-
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
428+
FMT_ENABLE_IF(is_compiled_string<S>::value)>
429429
constexpr auto compile(S fmt) {
430430
constexpr auto str = basic_string_view<typename S::char_type>(fmt);
431431
if constexpr (str.size() == 0) {
@@ -461,7 +461,7 @@ constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf,
461461
}
462462

463463
template <typename S, typename... Args,
464-
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
464+
FMT_ENABLE_IF(is_compiled_string<S>::value)>
465465
FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
466466
Args&&... args) {
467467
if constexpr (std::is_same<typename S::char_type, char>::value) {
@@ -488,7 +488,7 @@ FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
488488
}
489489

490490
template <typename OutputIt, typename S, typename... Args,
491-
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
491+
FMT_ENABLE_IF(is_compiled_string<S>::value)>
492492
FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
493493
constexpr auto compiled = detail::compile<Args...>(S());
494494
if constexpr (std::is_same<remove_cvref_t<decltype(compiled)>,
@@ -503,7 +503,7 @@ FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
503503
#endif
504504

505505
template <typename OutputIt, typename S, typename... Args,
506-
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
506+
FMT_ENABLE_IF(is_compiled_string<S>::value)>
507507
auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args)
508508
-> format_to_n_result<OutputIt> {
509509
using traits = detail::fixed_buffer_traits;
@@ -513,7 +513,7 @@ auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args)
513513
}
514514

515515
template <typename S, typename... Args,
516-
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
516+
FMT_ENABLE_IF(is_compiled_string<S>::value)>
517517
FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args)
518518
-> size_t {
519519
auto buf = detail::counting_buffer<>();
@@ -522,15 +522,15 @@ FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args)
522522
}
523523

524524
template <typename S, typename... Args,
525-
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
525+
FMT_ENABLE_IF(is_compiled_string<S>::value)>
526526
void print(std::FILE* f, const S& fmt, const Args&... args) {
527527
auto buf = memory_buffer();
528528
fmt::format_to(appender(buf), fmt, args...);
529529
detail::print(f, {buf.data(), buf.size()});
530530
}
531531

532532
template <typename S, typename... Args,
533-
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
533+
FMT_ENABLE_IF(is_compiled_string<S>::value)>
534534
void print(const S& fmt, const Args&... args) {
535535
print(stdout, fmt, args...);
536536
}

test/compile-test.cc

+12
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ TEST(compile_test, compile_format_string_literal) {
316316
}
317317
#endif
318318

319+
#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction)
320+
template <typename S>
321+
bool check_is_compiled_string(const S&) {
322+
return fmt::is_compiled_string<S>::value;
323+
}
324+
325+
TEST(compile_test, is_compiled_string) {
326+
EXPECT_TRUE(check_is_compiled_string(FMT_COMPILE("asdf")));
327+
EXPECT_TRUE(check_is_compiled_string(FMT_COMPILE("{}")));
328+
}
329+
#endif
330+
319331
// MSVS 2019 19.29.30145.0 - OK
320332
// MSVS 2022 19.32.31332.0, 19.37.32826.1 - compile-test.cc(362,3): fatal error
321333
// C1001: Internal compiler error.

0 commit comments

Comments
 (0)