如何让g++编译chrono输出?

huangapple go评论62阅读模式
英文:

How to make g++ compile a chrono output?

问题

我想在控制台中打印一个chrono时间点,我在以下stackoverflow问题中找到了解决方案:

https://stackoverflow.com/questions/15777073/how-do-you-print-a-c11-time-point

其中一个答案(https://stackoverflow.com/questions/15777073/how-do-you-print-a-c11-time-point/38449096#38449096)指出,在C++20(现在已经发布了一年多)中,可以将时间点放入输出命令中。不幸的是,我无法在我的机器上编译代码。我使用以下命令进行编译:

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

我使用这个命令是因为我读到了直接输出需要C++ 20支持。

文件“chrono5.cpp”包含与上面链接指向的答案完全相同的代码,该答案是由chrono库的创建者Howard Hinnant亲自编写的。我做错了什么?

错误消息非常长,所以我可能不应该完全包含它,但这是它的开头:

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>;}

我的编译器是否完全支持这个特性?非常感谢您的帮助!

英文:

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

https://stackoverflow.com/questions/15777073/how-do-you-print-a-c11-time-point

One of the answers (https://stackoverflow.com/questions/15777073/how-do-you-print-a-c11-time-point/38449096#38449096) 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&lt;&lt;’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream&lt;char&gt;’} and ‘std::chrono::_V2::system_clock::time_point’ {aka ‘std::chrono::time_point&lt;std::chrono::_V2::system_clock, std::chrono::duration&lt;long int, std::ratio&lt;1, 1000000000&gt; &gt; &gt;’})
    7 |     std::cout &lt;&lt; std::chrono::system_clock::now() &lt;&lt; &quot; UTC\n&quot;;
      |     ~~~~~~~~~ ^~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |          |                                     |
      |          |                                     std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point&lt;std::chrono::_V2::system_clock, std::chrono::duration&lt;long int, std::ratio&lt;1, 1000000000&gt; &gt; &gt;}
      |          std::ostream {aka std::basic_ostream&lt;char&gt;}

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

答案1

得分: 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 a 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, the 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).

英文:

The operator&lt;&lt; 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 &lt;chrono&gt; library. It introduces concepts of calendars and time zones which the &lt;chrono&gt; library didn't have before. These are necessary to e.g. print a time point as calendar date and day time as operator&lt;&lt; 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&lt;&lt; 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).

huangapple
  • 本文由 发表于 2023年2月6日 06:49:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定