mark formatter<>::format() functions as const

to revert back some changes in compile.h
This commit is contained in:
Alexey Ochapov 2020-12-22 22:31:55 +03:00
parent 2980dd3a6b
commit b8d973aaa0
No known key found for this signature in database
GPG Key ID: 9DC52E8F031B8DA8
2 changed files with 32 additions and 17 deletions

View File

@ -469,7 +469,7 @@ template <typename Char, typename T, int N> struct spec_field {
formatter<T, Char> fmt; formatter<T, Char> fmt;
template <typename OutputIt, typename... Args> template <typename OutputIt, typename... Args>
constexpr OutputIt format(OutputIt out, const Args&... args) { constexpr OutputIt format(OutputIt out, const Args&... args) const {
// This ensures that the argument type is convertile to `const T&`. // This ensures that the argument type is convertile to `const T&`.
const T& arg = get<N>(args...); const T& arg = get<N>(args...);
const auto& vargs = const auto& vargs =
@ -488,7 +488,7 @@ template <typename L, typename R> struct concat {
using char_type = typename L::char_type; using char_type = typename L::char_type;
template <typename OutputIt, typename... Args> template <typename OutputIt, typename... Args>
constexpr OutputIt format(OutputIt out, const Args&... args) { constexpr OutputIt format(OutputIt out, const Args&... args) const {
out = lhs.format(out, args...); out = lhs.format(out, args...);
return rhs.format(out, args...); return rhs.format(out, args...);
} }
@ -635,7 +635,7 @@ FMT_DEPRECATED auto compile(const Args&... args)
template <typename CompiledFormat, typename... Args, template <typename CompiledFormat, typename... Args,
typename Char = typename CompiledFormat::char_type, typename Char = typename CompiledFormat::char_type,
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)> FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
FMT_INLINE std::basic_string<Char> format(CompiledFormat& cf, FMT_INLINE std::basic_string<Char> format(const CompiledFormat& cf,
const Args&... args) { const Args&... args) {
basic_memory_buffer<Char> buffer; basic_memory_buffer<Char> buffer;
cf.format(detail::buffer_appender<Char>(buffer), args...); cf.format(detail::buffer_appender<Char>(buffer), args...);
@ -644,7 +644,7 @@ FMT_INLINE std::basic_string<Char> format(CompiledFormat& cf,
template <typename OutputIt, typename CompiledFormat, typename... Args, template <typename OutputIt, typename CompiledFormat, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)> FMT_ENABLE_IF(detail::is_compiled_format<CompiledFormat>::value)>
constexpr OutputIt format_to(OutputIt out, CompiledFormat& cf, constexpr OutputIt format_to(OutputIt out, const CompiledFormat& cf,
const Args&... args) { const Args&... args) {
return cf.format(out, args...); return cf.format(out, args...);
} }
@ -674,14 +674,14 @@ FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
return fmt::to_string(detail::first(args...)); return fmt::to_string(detail::first(args...));
} }
#endif #endif
auto compiled = detail::compile<Args...>(S()); constexpr auto compiled = detail::compile<Args...>(S());
return format(compiled, std::forward<Args>(args)...); return format(compiled, std::forward<Args>(args)...);
} }
template <typename OutputIt, typename CompiledFormat, typename... Args, template <typename OutputIt, typename CompiledFormat, typename... Args,
FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format, FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
CompiledFormat>::value)> CompiledFormat>::value)>
constexpr OutputIt format_to(OutputIt out, CompiledFormat& cf, constexpr OutputIt format_to(OutputIt out, const CompiledFormat& cf,
const Args&... args) { const Args&... args) {
using char_type = typename CompiledFormat::char_type; using char_type = typename CompiledFormat::char_type;
using context = format_context_t<OutputIt, char_type>; using context = format_context_t<OutputIt, char_type>;
@ -692,7 +692,7 @@ constexpr OutputIt format_to(OutputIt out, CompiledFormat& cf,
template <typename OutputIt, typename S, typename... Args, template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)> FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, const Args&... args) { FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, const Args&... args) {
auto compiled = detail::compile<Args...>(S()); constexpr auto compiled = detail::compile<Args...>(S());
return format_to(out, compiled, args...); return format_to(out, compiled, args...);
} }
@ -714,7 +714,7 @@ template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)> FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n, const S&, format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n, const S&,
const Args&... args) { const Args&... args) {
auto compiled = detail::compile<Args...>(S()); constexpr auto compiled = detail::compile<Args...>(S());
auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), compiled, auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), compiled,
args...); args...);
return {it.base(), it.count()}; return {it.base(), it.count()};

View File

@ -3539,6 +3539,20 @@ struct formatter<T, Char,
return it; return it;
} }
template <typename FormatContext>
FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) const
-> decltype(ctx.out()) {
auto specs = specs_;
detail::handle_dynamic_spec<detail::width_checker>(specs.width,
specs.width_ref, ctx);
detail::handle_dynamic_spec<detail::precision_checker>(
specs.precision, specs.precision_ref, ctx);
using af = detail::arg_formatter<typename FormatContext::iterator,
typename FormatContext::char_type>;
return visit_format_arg(af(ctx, nullptr, &specs),
detail::make_arg<FormatContext>(val));
}
template <typename FormatContext> template <typename FormatContext>
FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx)
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
@ -3556,13 +3570,14 @@ struct formatter<T, Char,
detail::dynamic_format_specs<Char> specs_; detail::dynamic_format_specs<Char> specs_;
}; };
#define FMT_FORMAT_AS(Type, Base) \ #define FMT_FORMAT_AS(Type, Base) \
template <typename Char> \ template <typename Char> \
struct formatter<Type, Char> : formatter<Base, Char> { \ struct formatter<Type, Char> : formatter<Base, Char> { \
template <typename FormatContext> \ template <typename FormatContext> \
auto format(Type const& val, FormatContext& ctx) -> decltype(ctx.out()) { \ auto format(Type const& val, FormatContext& ctx) const \
return formatter<Base, Char>::format(static_cast<Base>(val), ctx); \ -> decltype(ctx.out()) { \
} \ return formatter<Base, Char>::format(static_cast<Base>(val), ctx); \
} \
} }
FMT_FORMAT_AS(signed char, int); FMT_FORMAT_AS(signed char, int);
@ -3582,7 +3597,7 @@ FMT_FORMAT_AS(std::byte, unsigned);
template <typename Char> template <typename Char>
struct formatter<void*, Char> : formatter<const void*, Char> { struct formatter<void*, Char> : formatter<const void*, Char> {
template <typename FormatContext> template <typename FormatContext>
auto format(void* val, FormatContext& ctx) -> decltype(ctx.out()) { auto format(void* val, FormatContext& ctx) const -> decltype(ctx.out()) {
return formatter<const void*, Char>::format(val, ctx); return formatter<const void*, Char>::format(val, ctx);
} }
}; };
@ -3590,7 +3605,7 @@ struct formatter<void*, Char> : formatter<const void*, Char> {
template <typename Char, size_t N> template <typename Char, size_t N>
struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> { struct formatter<Char[N], Char> : formatter<basic_string_view<Char>, Char> {
template <typename FormatContext> template <typename FormatContext>
FMT_CONSTEXPR auto format(const Char* val, FormatContext& ctx) FMT_CONSTEXPR auto format(const Char* val, FormatContext& ctx) const
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
return formatter<basic_string_view<Char>, Char>::format(val, ctx); return formatter<basic_string_view<Char>, Char>::format(val, ctx);
} }