1

I would like to print a chrono time point in the console, and I've found the following stackoverflow question:

How do you print a C++11 time_point?

One of the answers (How do you print a C++11 time_point?) states that in C++20 (which is now out for more than a year) one can just put the time point into an output command. Unfortunately, I can't compile the code on my machine. I have used the following command for the compilation:

g++ -std=c++20 chrono5.cpp -o test

I use this command because I read that C++ 20 is required for the direct output to work.

The file "chrono5.cpp" contains the exact same code as the (edit to the) answer to which the link above points, which was literally written by Howard Hinnant, the creator of the chrono library. What am I doing wrong?

The error message is exceedingly long, whence I probably should not include it in its entirety, but here is its beginning:

chrono5.cpp:7:15: error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::chrono::_V2::system_clock::time_point’ {aka ‘std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >’})
    7 |     std::cout << std::chrono::system_clock::now() << " UTC\n";
      |     ~~~~~~~~~ ^~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |          |                                     |
      |          |                                     std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >}
      |          std::ostream {aka std::basic_ostream<char>}

Does my compiler have the support for this feature at all? Thank you very much in advance!

1 Answers1

5

The operator<< overload necessary to make this work was introduced in C++20 with the proposal P0355, which however also contains a much larger extension of the <chrono> library. It introduces concepts of calendars and time zones which the <chrono> library didn't have before. These are necessary to e.g. print a time point as calendar date and day time as operator<< now does.

literally written by Howard Hinnant, the creator of the chrono library

He designed the library and wrote a reference implementation, but every C++ implementation still has to implement the library specification for itself.

Does my compiler have the support for this feature at all? Thank you very much in advance!

It is not about compiler support, but standard library support. Part of GCC is libstdc++ which contains the standard library implementation. On libstdc++'s implementation status page the paper I mentioned earlier is still listed as not implemented in any released version. But as @Brian notes in a comment under the question, current GCC trunk does support at least the operator<< overload you need here.

For an overview of multiple standard library implementations you can see https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_library_features, which currently lists libstdc++'s implementation of the paper as partial since GCC 11 and targeted as complete for the next major release GCC 13.

LLVM's libc++ is also listed as partial (since LLVM/Clang 7) and only MSVC's implementation is listed as currently complete in a released version (since 16.10).

user17732522
  • 53,019
  • 2
  • 56
  • 105
  • Hmm, so I did find P0355 on the libc++20 page, where it says it's "in progress". Furthermore, if the previous release dates of gcc are of any predictive value, they will not release gcc13 before spring. Thus, I will probably have to build a git clone (I sure hope one doesn't need gcc13 to do it). – AlgebraicsAnonymous Feb 06 '23 at 12:01
  • @AlgebraicsAnonymous The libstdc++ version is normally bound to the GCC version. Normally both are build and installed together. I am not sure whether it is possible to mismatch them. But building a recent GCC/libstdc++ should be possible with quite old versions of GCC, I think something like 4.6, without any intermediate steps. – user17732522 Feb 06 '23 at 17:01
  • I'm not quite sure if I follow. Are these two developed together or no? Can I mismatch them or no? – AlgebraicsAnonymous Feb 07 '23 at 21:34
  • @AlgebraicsAnonymous They are developed as part of the same project and as one source, but they can be compiled/installed individually. I don't know whether GCC and libstdc++ can be mismatched, I never tried. Certainly newer libstdc++ might use newer compiler features/built-ins, so that there might be a compatibility issue using an old GCC compiler version. The point is just that the standard library implementation can be considered a distinct component. For example the LLVM sources contain Clang and libc++ as components, but Clang also supports libstdc++ as an alternative to libc++. – user17732522 Feb 07 '23 at 22:30