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

Can I have a specialization of a range formatter? #4323

Closed
Sebanisu opened this issue Jan 23, 2025 · 2 comments
Closed

Can I have a specialization of a range formatter? #4323

Sebanisu opened this issue Jan 23, 2025 · 2 comments
Labels

Comments

@Sebanisu
Copy link

Might be related to #4123

I noticed if I include fmt/ranges.h this formatter will no longer work. It's a wip formatter. my tile formatter puts out too much data. So I wanted to make a summery output for when formatting the range. I was thinking it'd be nice to have both as I'd like to put some ranges of numbers into the output sometimes.

I thought since my concept is specific to only tile ranges. It would override the one in fmt/ranges.h. But that isn't the case. Maybe it's because you use SFINAE or std::enable_if.

template<typename range_t>
concept tile_range = std::ranges::range<range_t> && open_viii::graphics::background::is_tile<std::ranges::range_value_t<range_t>>;

// Specialization for ranges of tiles
template<tile_range TileRange>
struct fmt::formatter<TileRange> : fmt::formatter<std::string>
{
     // parse is inherited from formatter<string_view>.
     template<typename FormatContext>
     constexpr auto format(const TileRange &tiles, FormatContext &ctx) const
     {
          const auto count = std::ranges::distance(tiles);


          return fmt::format_to(ctx.out(), "Total Tiles: {}\n", count);
     }
};
@vitaut
Copy link
Contributor

vitaut commented Jan 23, 2025

You should be able to opt out of range formatting by specializing fmt::is_range: https://github.com/fmtlib/fmt/blob/41539c29f342e67abb925b946acab6d55d331b36/include/fmt/ranges.h#L346C45-L346C53.

@Sebanisu
Copy link
Author

template<tile_range TileRange>
struct fmt::is_range<TileRange, char> : std::false_type
{
};

Ah, I see. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants