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

Speculatively implement LWG-3636: formatter<T>::format should be const-qualified #2573

Merged
merged 3 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions stl/inc/chrono
Original file line number Diff line number Diff line change
Expand Up @@ -5956,7 +5956,7 @@ namespace chrono {
}

template <class _FormatContext, class _Ty>
_NODISCARD auto _Write(_FormatContext& _FormatCtx, const _Ty& _Val, const tm& _Time) {
_NODISCARD auto _Write(_FormatContext& _FormatCtx, const _Ty& _Val, const tm& _Time) const {
basic_ostringstream<_CharT> _Stream;

_Stream.imbue(_Specs._Localized ? _FormatCtx.locale() : locale::classic());
Expand Down Expand Up @@ -6004,7 +6004,7 @@ namespace chrono {
// "40 is not a valid day".
template <class _Ty>
bool _Custom_write(
basic_ostream<_CharT>& _Os, const _Chrono_spec<_CharT>& _Spec, const tm& _Time, const _Ty& _Val) {
basic_ostream<_CharT>& _Os, const _Chrono_spec<_CharT>& _Spec, const tm& _Time, const _Ty& _Val) const {
if constexpr (is_same_v<_Ty, local_info>) {
if (_Val.result != local_info::unique) {
_THROW(format_error("Cannot print non-unique local_info"));
Expand Down Expand Up @@ -6334,7 +6334,7 @@ struct _Fill_tm_formatter {
}

template <class _FormatContext>
auto format(const _Ty& _Val, _FormatContext& _FormatCtx) {
auto format(const _Ty& _Val, _FormatContext& _FormatCtx) const {
return _Impl._Write(_FormatCtx, _Val, _Fill_tm(_Val));
}

Expand Down Expand Up @@ -6425,7 +6425,7 @@ struct formatter<_CHRONO sys_time<_Duration>, _CharT> {
}

template <class _FormatContext>
auto format(const _CHRONO sys_time<_Duration>& _Val, _FormatContext& _FormatCtx) {
auto format(const _CHRONO sys_time<_Duration>& _Val, _FormatContext& _FormatCtx) const {
return _Impl._Write(_FormatCtx, _Val, _Fill_tm(_Val));
}

Expand All @@ -6440,7 +6440,7 @@ struct formatter<_CHRONO utc_time<_Duration>, _CharT> {
}

template <class _FormatContext>
auto format(const _CHRONO utc_time<_Duration>& _Val, _FormatContext& _FormatCtx) {
auto format(const _CHRONO utc_time<_Duration>& _Val, _FormatContext& _FormatCtx) const {
const auto _Sys = _CHRONO utc_clock::to_sys(_Val);
return _Impl._Write(_FormatCtx, _Val, _Fill_tm(_Sys));
}
Expand All @@ -6456,7 +6456,7 @@ struct formatter<_CHRONO tai_time<_Duration>, _CharT> {
}

template <class _FormatContext>
auto format(const _CHRONO tai_time<_Duration>& _Val, _FormatContext& _FormatCtx) {
auto format(const _CHRONO tai_time<_Duration>& _Val, _FormatContext& _FormatCtx) const {
using namespace chrono;
using _Common = common_type_t<_Duration, days>; // slightly optimize by performing conversion at compile time
constexpr _Common _Offset{sys_days{year{1970} / January / 1} - sys_days{year{1958} / January / 1}};
Expand All @@ -6475,7 +6475,7 @@ struct formatter<_CHRONO gps_time<_Duration>, _CharT> {
}

template <class _FormatContext>
auto format(const _CHRONO gps_time<_Duration>& _Val, _FormatContext& _FormatCtx) {
auto format(const _CHRONO gps_time<_Duration>& _Val, _FormatContext& _FormatCtx) const {
using namespace chrono;
using _Common = common_type_t<_Duration, days>; // slightly optimize by performing conversion at compile time
constexpr _Common _Offset{sys_days{year{1980} / January / Sunday[1]} - sys_days{year{1970} / January / 1}};
Expand All @@ -6494,7 +6494,7 @@ struct formatter<_CHRONO file_time<_Duration>, _CharT> {
}

template <class _FormatContext>
auto format(const _CHRONO file_time<_Duration>& _Val, _FormatContext& _FormatCtx) {
auto format(const _CHRONO file_time<_Duration>& _Val, _FormatContext& _FormatCtx) const {
const auto _Utc = _CHRONO file_clock::to_utc(_Val);
const auto _Sys = _CHRONO utc_clock::to_sys(_Utc);
return _Impl._Write(_FormatCtx, _Utc, _Fill_tm(_Sys));
Expand All @@ -6517,7 +6517,7 @@ struct formatter<_CHRONO zoned_time<_Duration, _TimeZonePtr>, _CharT>
: formatter<_CHRONO _Local_time_format_t<_Duration>, _CharT> {

template <class _FormatContext>
auto format(const _CHRONO zoned_time<_Duration, _TimeZonePtr>& _Val, _FormatContext& _FormatCtx) {
auto format(const _CHRONO zoned_time<_Duration, _TimeZonePtr>& _Val, _FormatContext& _FormatCtx) const {
using _Mybase = formatter<_CHRONO _Local_time_format_t<_Duration>, _CharT>;
const auto _Info = _Val.get_info();
return _Mybase::format({_Val.get_local_time(), &_Info.abbrev, &_Info.offset}, _FormatCtx);
Expand Down
9 changes: 5 additions & 4 deletions stl/inc/format
Original file line number Diff line number Diff line change
Expand Up @@ -2879,20 +2879,21 @@ struct _Formatter_base {
}

template <class _FormatContext>
typename _FormatContext::iterator format(const _Ty& _Val, _FormatContext& _FormatCtx) {
typename _FormatContext::iterator format(const _Ty& _Val, _FormatContext& _FormatCtx) const {
_Dynamic_format_specs<_CharT> _Format_specs = _Specs;
if (_Specs._Dynamic_width_index >= 0) {
_Specs._Width =
_Format_specs._Width =
_Get_dynamic_specs<_Width_checker>(_FormatCtx.arg(static_cast<size_t>(_Specs._Dynamic_width_index)));
}

if (_Specs._Dynamic_precision_index >= 0) {
_Specs._Precision = _Get_dynamic_specs<_Precision_checker>(
_Format_specs._Precision = _Get_dynamic_specs<_Precision_checker>(
_FormatCtx.arg(static_cast<size_t>(_Specs._Dynamic_precision_index)));
}

return _STD visit_format_arg(
_Arg_formatter<typename _FormatContext::iterator, _CharT>{
._Ctx = _STD addressof(_FormatCtx), ._Specs = _STD addressof(_Specs)},
._Ctx = _STD addressof(_FormatCtx), ._Specs = _STD addressof(_Format_specs)},
basic_format_arg<_FormatContext>{_Val});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct std::formatter<basic_custom_formattable_type, char> {
}
return parse_ctx.end();
}
format_context::iterator format(const basic_custom_formattable_type& val, format_context& ctx) {
format_context::iterator format(const basic_custom_formattable_type& val, format_context& ctx) const {
ctx.advance_to(copy(val.string_content.begin(), val.string_content.end(), ctx.out()));
return ctx.out();
}
Expand All @@ -77,7 +77,7 @@ struct std::formatter<not_const_formattable_type, char> {
}
return parse_ctx.end();
}
format_context::iterator format(not_const_formattable_type& val, format_context& ctx) {
format_context::iterator format(not_const_formattable_type& val, format_context& ctx) const {
ctx.advance_to(copy(val.string_content.begin(), val.string_content.end(), ctx.out()));
return ctx.out();
}
Expand All @@ -91,7 +91,7 @@ struct custom_formattable_type {
template <class T, class charT>
struct std::formatter<custom_formattable_type<T>, charT> : std::formatter<T, charT> {
template <class FC>
auto format(const custom_formattable_type<T>& val, FC& format_ctx) {
auto format(const custom_formattable_type<T>& val, FC& format_ctx) const {
return std::formatter<T, charT>::format(val.val, format_ctx);
}
};
Expand Down