From dc56728e3ae328d28f3ccba4392475b08e91547b Mon Sep 17 00:00:00 2001 From: DV Henkel-Wallace Date: Thu, 3 Sep 2020 20:14:17 -0700 Subject: [PATCH] Support printing of filtered views by removing const constraint Filtered views cannot be const and have no const iterators Note: Transformed views can be const but don't have const iterators either. The change in ranges.h `formatter::value>>::format` is relatively benign for this reason. However constness is cast away from the argument to `value::format_custom_arg` which is in core.h -- seeminglyl pretty harmless in a private method whout a callback. --- include/fmt/core.h | 5 ++++- include/fmt/ranges.h | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index d2c9633c..4730201a 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1097,7 +1097,10 @@ template class value { Context& ctx) { Formatter f; parse_ctx.advance_to(f.parse(parse_ctx)); - ctx.advance_to(f.format(*static_cast(arg), ctx)); + // We cast away any constness in order to print filtered views (ranges) + // which cannot be const + // This is a private method and we know that won't modify the object + ctx.advance_to(f.format(*const_cast(static_cast(arg)), ctx)); } }; diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index c48f1727..21e8d344 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -257,7 +257,7 @@ struct formatter - typename FormatContext::iterator format(const RangeT& values, + typename FormatContext::iterator format(RangeT& values, FormatContext& ctx) { auto out = detail::copy(formatting.prefix, ctx.out()); size_t i = 0;