简体   繁体   中英

Difference between boost::chrono::time_point<> and boost::chrono::steady_clock::time_point

What is the difference between boost::chrono::steady_clock::time_point and boost::chrono::time_point<boost::chrono::steady_clock,Duration> , and why can't they be converted between each other?

It seems extremely redundant.

They can be converted between each other if their durations are the same. For example, the following would be fine:

boost::chrono::steady_clock::time_point tp1 = boost::chrono::steady_clock::now();
boost::chrono::time_point<boost::chrono::steady_clock, 
                              boost::chrono::nanoseconds>
tp2 = tp1;

You can also use time_point_cast to cast one time_point to another if their durations are different.

Example

boost::chrono::steady_clock::time_point tp1 = boost::chrono::steady_clock::now();
boost::chrono::time_point<boost::chrono::steady_clock,
                              boost::chrono::milliseconds> 
tp2 = boost::chrono::time_point_cast<boost::chrono::milliseconds>(tp1);

Also, I'm not sure why you think they are redundant. If you look at the docs , steady_clock has the following member:

typedef chrono::time_point<steady_clock> time_point;

So, boost::chrono::steady_clock::time_point is just a typedef for boost::chrono::time_point<steady_clock> .

std::chrono::time_point (based on Boost) is a template that can be used to define a class that can, in turn, be used to define a point in time. std::chrono::steady_clock::time_point is an instantiation of that template (ie, a class) that can be used to define a point in time. So they're two rather different things with the same name. Sigh.

Various instantiations of std::chrono::time_point can, as @JesseGood pointed out, be inter-converted with time_point_cast .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM