Make const-correct

This commit is contained in:
Shawn Zhong 2023-02-07 03:24:39 -06:00
parent ef3ffef853
commit 5e9dfa354c
2 changed files with 3 additions and 2 deletions

View File

@ -678,9 +678,9 @@ struct formatter<T, Char,
enable_if_t<detail::is_container_adaptor_like<T>::value>> enable_if_t<detail::is_container_adaptor_like<T>::value>>
: formatter<typename T::container_type, Char> { : formatter<typename T::container_type, Char> {
template <typename FormatContext> template <typename FormatContext>
auto format(T& t, FormatContext& ctx) const -> decltype(ctx.out()) { auto format(const T& t, FormatContext& ctx) const -> decltype(ctx.out()) {
struct getter : T { 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. return t.*(&getter::c); // Access c through the derived class.
} }
}; };

View File

@ -424,6 +424,7 @@ TEST(ranges_test, container_adaptor) {
s.push(1); s.push(1);
s.push(2); s.push(2);
EXPECT_EQ(fmt::format("{}", s), "[1, 2]"); EXPECT_EQ(fmt::format("{}", s), "[1, 2]");
EXPECT_EQ(fmt::format("{}", const_cast<const decltype(s)&>(s)), "[1, 2]");
} }
{ {