diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 8e24aba2..fae425f9 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -678,9 +678,9 @@ struct formatter::value>> : formatter { template - auto format(T& t, FormatContext& ctx) const -> decltype(ctx.out()) { + auto format(const T& t, FormatContext& ctx) const -> decltype(ctx.out()) { struct getter : T { - static auto get(T& t) -> typename T::container_type& { + static auto get(const T& t) -> const typename T::container_type& { return t.*(&getter::c); // Access c through the derived class. } }; diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 0ce88ba4..e1d384d7 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -424,6 +424,7 @@ TEST(ranges_test, container_adaptor) { s.push(1); s.push(2); EXPECT_EQ(fmt::format("{}", s), "[1, 2]"); + EXPECT_EQ(fmt::format("{}", const_cast(s)), "[1, 2]"); } {