Skip to content
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

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

Closed
AnthonyVH opened this issue Aug 2, 2024 · 3 comments
Closed

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

AnthonyVH opened this issue Aug 2, 2024 · 3 comments

Comments

@AnthonyVH
Copy link
Contributor

AnthonyVH commented Aug 2, 2024

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
Copy link
Contributor

vitaut commented Aug 2, 2024

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
Copy link
Contributor

phprus commented Aug 2, 2024

Fix: #4103

@AnthonyVH
Copy link
Contributor Author

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

@vitaut vitaut closed this as completed Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants