https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121705

            Bug ID: 121705
           Summary: When using import std, formatting
                    std::chrono::year_month_day will cause compilation
                    failure
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: printfne at gmail dot com
  Target Milestone: ---

After using import std instead of include, when attempting to format
std::chrono::year_month_day in the default format using cout, format,
stringstream, or println, it will result in compilation failure

/// mian.cpp
import std;

int main() {

    std::chrono::year_month_day ymd{};
    auto str = std::format("{}", ymd); // 1. error

    std::cout << ymd << std::endl;     // 2. error

    std::stringstream ss;
    ss << ymd;                         // 3. error

}


// command
g++-latest -std=c++26 -fmodules -c -fsearch-include-path bits/std.cc
g++-latest -std=c++26 -fmodules main.cpp



// error message
☁  ~ g++-latest -std=c++26 -fmodules -c -fsearch-include-path bits/std.cc
☁  ~ g++-latest -std=c++26 -fmodules -c main.cpp                         
In file included from /home/hexne/gcc-latest/include/c++/16.0.0/chrono:3352,
                 from
/home/hexne/gcc-latest/include/c++/16.0.0/x86_64-pc-linux-gnu/bits/stdc++.h:175,
                 from /home/hexne/gcc-latest/include/c++/16.0.0/bits/std.cc:30,
of module std, imported at main.cpp:1:
/home/hexne/gcc-latest/include/c++/16.0.0/bits/chrono_io.h: In instantiation of
'typename std::basic_format_context<_Out, _CharT>::iterator
std::formatter<std::chrono::year_month_day, _CharT>::format(const
std::chrono::year_month_day&, std::basic_format_context<_Out, _CharT>&) const
[with _Out = std::__format::_Sink_iter<char>; _CharT = char; typename
std::basic_format_context<_Out, _CharT>::iterator =
std::basic_format_context<std::__format::_Sink_iter<char>, char>::iterator]':
required from 'static void
std::basic_format_arg<_Context>::handle::_S_format(std::basic_format_parse_context<typename
_Context::char_type>&, _Context&, const void*) [with _Tq = const
std::chrono::year_month_day; _Context =
std::basic_format_context<std::__format::_Sink_iter<char>, char>; typename
_Context::char_type = char]'
/home/hexne/gcc-latest/include/c++/16.0.0/format:4085:40:   
 4085 |             __format_ctx.advance_to(__f.format(__val, __format_ctx));
      |                                     ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
required from 'std::basic_format_arg<_Context>::handle::handle(_Tp&) [with _Tp
= std::chrono::year_month_day; _Context =
std::basic_format_context<std::__format::_Sink_iter<char>, char>]'
/home/hexne/gcc-latest/include/c++/16.0.0/format:4093:11:   
 4093 |             auto __func = _S_format<__maybe_const_t<_Tp>>;
      |                  ^~~~~~
required from 'std::basic_format_arg<_Context>::basic_format_arg(_Tp&) [with
_Tp = std::chrono::year_month_day; _Context =
std::basic_format_context<std::__format::_Sink_iter<char>, char>]'
/home/hexne/gcc-latest/include/c++/16.0.0/format:4324:13:   
 4324 |             _M_set(static_cast<_Td>(__v));
      |                    ^~~~~~~~~~~~~~~~~~~~~
required from 'static std::__format::_Arg_store<_Context, _Args>::_Element_t
std::__format::_Arg_store<_Context, _Args>::_S_make_elt(_Tp&) [with _Tp =
std::chrono::year_month_day; _Context =
std::basic_format_context<std::__format::_Sink_iter<char>, char>; _Args =
{std::basic_format_arg<std::basic_format_context<std::__format::_Sink_iter<char>,
char> >::handle}; _Element_t =
std::__format::_Arg_store<std::basic_format_context<std::__format::_Sink_iter<char>,
char>,
std::basic_format_arg<std::basic_format_context<std::__format::_Sink_iter<char>,
char> >::handle>::_Element_t]'
/home/hexne/gcc-latest/include/c++/16.0.0/format:4632:31:   
 4632 |           basic_format_arg<_Context> __arg(__v);
      |                                      ^~~~~
required from 'std::__format::_Arg_store<_Context, _Args>::_Arg_store(_Tp& ...)
[with _Tp = {std::chrono::year_month_day}; _Context =
std::basic_format_context<std::__format::_Sink_iter<char>, char>; _Args =
{std::basic_format_arg<std::basic_format_context<std::__format::_Sink_iter<char>,
char> >::handle}]'
/home/hexne/gcc-latest/include/c++/16.0.0/format:4643:23:   
 4643 |         : _M_args{_S_make_elt(__a)...}
      |                   ~~~~~~~~~~~^~~~~
required from 'auto std::make_format_args(_Args& ...) [with _Context =
basic_format_context<__format::_Sink_iter<char>, char>; _Args =
{chrono::year_month_day}]'
/home/hexne/gcc-latest/include/c++/16.0.0/format:4693:14:   
 4693 |       return _Store(__fmt_args...);
      |              ^~~~~~~~~~~~~~~~~~~~~
required from 'std::string std::format(format_string<_Args ...>, _Args&& ...)
[with _Args = {chrono::year_month_day&}; string = __cxx11::basic_string<char>;
format_string<_Args ...> = basic_format_string<char, chrono::year_month_day&>]'
/home/hexne/gcc-latest/include/c++/16.0.0/format:5246:61:   
 5246 |     { return std::vformat(__fmt.get(),
std::make_format_args(__args...)); }
      |                                        ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
required from here
main.cpp:6:27:   
    6 |     auto str = std::format("{}", ymd); // 1. error
      |                ~~~~~~~~~~~^~~~~~~~~~~
/home/hexne/gcc-latest/include/c++/16.0.0/bits/chrono_io.h:2643:23: error: no
match for 'operator==' (operand types are 'std::__format::_ChronoParts' and
'int')
 2643 |           if (__parts == 0)
      |               ~~~~~~~~^~~~


This problem does not exist in gcc 15.2.1 20250813. It can be reproduced using
gcc 16.0.0 20250828 (experimental)

Reply via email to