Search code examples
c++11pass-by-referenceconstexprpass-by-value

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?


Solution

  • 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).