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!
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).