fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
20.43k stars 2.45k forks source link

non-constexpr formatted_size() breaks compile-time formatting #4102

Closed AnthonyVH closed 1 month ago

AnthonyVH commented 1 month ago

Before f6b4a23b83 the function formatted_size() in compile.h was marked as FMT_CONSTEXPR20.

This allowed writing compile-time formatting using C++23's static constexpr members inside a constexpr function. E.g.:

template <fmt::detail::udl_compiled_string format, auto... values>
constexpr std::string_view constexpr_format() {
  static constexpr auto str = [] {
    constexpr auto result_length = fmt::formatted_size(format, values...);
    auto result = std::array<char, result_length>{};
    fmt::format_to(result.data(), format, values...);
    return result;
  }();
  return std::string_view(str.data(), str.size());
}

However, since the linked commit, this annotation was removed. Looking at the commit, I can't figure out why this is the case. Is there any chance of re-introducing the constexpr on all functions that allow it in compile.h?

PS: fmt::counting_buffer::count() is not marked constexpr either. When I wrote the snippet above, an fmt::counting_iterator was used instead in fmt::formatted_size() (but this iterator was removed in ba36a0481).

vitaut commented 1 month ago

It was relying on deprecated and inefficient counting_iterator which was removed. A PR to constexprify counting_buffer and reintroduce constexpr to formatted_size would be welcome.

phprus commented 1 month ago

Fix: #4103

AnthonyVH commented 1 month ago

I was just about to prepare a PR, but you beat me to it :smile:. Thanks a lot for the super quick fix!