Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator= between two std::chrono::time_point cause error

Tags:

c++

c++-chrono

I have a struct which contain a std::chrono::system_clock::time_point

struct NetInfo {
    std::chrono::system_clock::time_point time;
    std::chrono::steady_clock::time_point start;
};

And when I try to assign one of the time_point to the result of std::chrono::system_clock::now()

    api::NetInfo ni;
    ni.start = std::chrono::system_clock::now();

I get this huge error

    /home/rootkid/rendu/cpp2/cpp_zia/lib/NetService/src/NetService.cpp:48:51: error: no match for ‘operator=’ (operand types are ‘std::chrono::_V2::steady_clock::time_point {aka std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >}’ 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> > >}’)
         ni.start = std::chrono::system_clock::now();
                                                   ^
In file included from /usr/include/c++/7.2.1/thread:38:0,
                 from /home/rootkid/rendu/cpp2/cpp_zia/lib/NetService/./include/NetService.h:3,
                 from /home/rootkid/rendu/cpp2/cpp_zia/lib/NetService/src/NetService.cpp:1:
/usr/include/c++/7.2.1/chrono:610:14: note: candidate: constexpr std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >& std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >::operator=(const std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&)
       struct time_point
              ^~~~~~~~~~
/usr/include/c++/7.2.1/chrono:610:14: note:   no known conversion for argument 1 from ‘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> > >}’ to ‘const std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&’
/usr/include/c++/7.2.1/chrono:610:14: note: candidate: constexpr std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >& std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >::operator=(std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&&)
/usr/include/c++/7.2.1/chrono:610:14: note:   no known conversion for argument 1 from ‘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> > >}’ to ‘std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&&’

Any Idea of what am I doing wrong here ?

like image 384
Théo Champion Avatar asked Jun 01 '26 17:06

Théo Champion


1 Answers

Though not specified, it is a de-facto standard that system_clock is measuring Unix Time (approximately time duration since 1970-01-01 00:00:00 UTC), though with differing precisions on each platform.

The epoch of steady_clock is also unspecified, and no de-facto standard has evolved. Different platforms do different things. For example on my platform steady_clock measures the time since the computer booted up.

<chrono> was designed to catch as many logic errors as possible at compile-time. It would be a logic error to assign a system_clock::time_point to a steady_clock::time_point since these two clocks are measuring against different epochs.

A future standard might offer a way to "transform" or "cast" one clock's time_point to another, but it won't be a straightforward assignment. It would have to involve some computation which takes into account the difference in the epochs. Otherwise the time_point would represent a different instant in time after the assignment compared to before the assignment.

tl;dr: The compiler has caught a logic error for you at compile-time.

like image 174
Howard Hinnant Avatar answered Jun 03 '26 05:06

Howard Hinnant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!