1

I am using c++ 20 on visual studio . It says no operator found which takes a right hand operand of type std::chrono::time_point following error on following code.

    std::chrono::time_point currTime = std::chrono::system_clock::now();
    cout << currTime<< endl;

lorem1213
  • 433
  • 4
  • 11
  • 4
    The compiler is correct, there's no "output" operator overloaded for [`time_point`](https://en.cppreference.com/w/cpp/chrono/time_point). You can [convert it to a `time_t`](https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t) and from that get [a `std::tm` pointer](https://en.cppreference.com/w/cpp/chrono/c/tm) to use for [`std::put_time`](https://en.cppreference.com/w/cpp/io/manip/put_time). Yes it's rather messy, but if you want to limit yourself to the standard library facilities that's what you have to do. – Some programmer dude Jul 18 '22 at 10:53
  • 1
    see eg the example here: https://en.cppreference.com/w/cpp/chrono/time_point – 463035818_is_not_an_ai Jul 18 '22 at 11:04
  • So, the mods just points me to the question where the only answer suggests using the library. – lorem1213 Jul 18 '22 at 11:13
  • At least you used the only chrono clock that can be converted to a time_t easily. Otherwise, picking correct dupes (and close reasons for that matter) is not a strong suit here. – sweenish Jul 18 '22 at 11:25
  • 1
    Are you sure you're using c++20? It should work: https://godbolt.org/z/3c1Mfoad7 – Alan Birtles Jul 18 '22 at 11:31
  • It's worth noting that if you have access to C++20 (and the feature's been implemented by the compiler), you can print more directly when using chrono. [Link](https://en.cppreference.com/w/cpp/chrono/system_clock/formatter). I've voted to re-open so someone or myself can eventually provide an answer, but that link has the information. – sweenish Jul 18 '22 at 11:31
  • 2
    There's also [the `fmt` library](https://fmt.dev/latest/index.html) (which have mostly been incorporated into the C++20 standard as [`std::format`](https://en.cppreference.com/w/cpp/utility/format/format)). *And* it's been added in the C++20 standard. Neither `std::format` not direct output is well-supported in Clang or GCC, but direct output works with MSVC ([tested with version 19.32](https://godbolt.org/z/sPf5145qa)). – Some programmer dude Jul 18 '22 at 11:34
  • @AlanBirtles I have langauge standard set to `ISO C++20 Standard (/std:c++20)` but its not working. – lorem1213 Jul 18 '22 at 11:41
  • Which version of visual studio? What is the compiler command line? Please show a [mre] – Alan Birtles Jul 18 '22 at 11:50
  • 2
    Versions of MSVC before 19.32 needs `/std:c++latest` instead. But it seems to bug-out with version 19.31 ("internal error"), and only works down to version 19.29 (VS16.10). – Some programmer dude Jul 18 '22 at 11:51
  • My version is 171.2. Gotta update. – lorem1213 Jul 18 '22 at 12:04
  • Please use correct `std::chrono::system_clock::now()` and `std::chrono::high_resolution_clock::now()`. `std::chrono::time_point currTime` should be clear `std::chrono::steady_clock::time_point currTime` or `std::chrono::system_clock::time_point currTime` are differ. – trung Aug 05 '23 at 19:04

0 Answers0