按值或引用 const 传递 std::chrono::duration?
Pass std::chrono::duration by value or by reference to const?
所以过去我通过引用 const 传递了 std::chrono::duration
值,但现在我认为它们只不过是包裹在 class 中的单一算术类型,所以传递它是有意义的值。
有人介绍过吗?
根据规范,duration 包含
中指定的 Rep 类型的单个变量(刻度数)
template<
class Rep,
class Period = std::ratio<1>
> class duration;
所以基本上您可以选择处理 copying/passing const ref,就像您对该 Rep 类型所做的那样。
当我以前使用 chromo::duration 时,我选择它是一个 long,在那种情况下,我没有看到通过 const 引用传递的理由,(尤其是在我的体系结构中,一个 long 和一个地址都是 8 个字节)
I think they are no more than a single arithmetic type wrapped
看看cppreference.com :
The only data stored in a duration is a tick count of type Rep. If Rep is floating point, then the duration can represent fractions of ticks. Period is included as part of the duration's type, and is only used when converting between different durations.
和:
where the tick period is a compile-time rational constant
你猜对了
所以过去我通过引用 const 传递了 std::chrono::duration
值,但现在我认为它们只不过是包裹在 class 中的单一算术类型,所以传递它是有意义的值。
有人介绍过吗?
根据规范,duration 包含
中指定的 Rep 类型的单个变量(刻度数)template<
class Rep,
class Period = std::ratio<1>
> class duration;
所以基本上您可以选择处理 copying/passing const ref,就像您对该 Rep 类型所做的那样。 当我以前使用 chromo::duration 时,我选择它是一个 long,在那种情况下,我没有看到通过 const 引用传递的理由,(尤其是在我的体系结构中,一个 long 和一个地址都是 8 个字节)
I think they are no more than a single arithmetic type wrapped
看看cppreference.com :
The only data stored in a duration is a tick count of type Rep. If Rep is floating point, then the duration can represent fractions of ticks. Period is included as part of the duration's type, and is only used when converting between different durations.
和:
where the tick period is a compile-time rational constant
你猜对了