Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't std::chrono::duration instances passed by value in the Standard Library?

According to cppreference.com:

The only data stored in a duration is a tick count of type Rep.

However, I've noticed that for example:

this_thread::sleep_for takes sleep_duration by const ref.

future::wait_for likewise takes duration by const ref.

etc.

(1) Any particular reason why they are not passed by value instead?

(1a) Does passing by const reference pessimize some possible constexpr optimizations?

like image 840
Innocent Bystander Avatar asked Oct 18 '25 21:10

Innocent Bystander


1 Answers

It was a judgement call. The cost for the common case of the "built-in" durations is small compared to the cost of what the function is going to do (sleep). And I didn't want to think about the cost of pass-by-value for a custom duration containing a custom Rep that might be expensive to copy (e.g. BigNum).

like image 136
Howard Hinnant Avatar answered Oct 21 '25 19:10

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!