什么是std :: chrono :: duration缺乏立即计数操作的背后的原因? [英] What is the reason behind std::chrono::duration's lack of immediate tick count manipulation?

查看:209
本文介绍了什么是std :: chrono :: duration缺乏立即计数操作的背后的原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有

  #include< chrono> 
#include< iostream>
#include< ctime>

namespace比例{typedef std :: ratio< 60 * 60 * 24,1>天; }

typedef std :: chrono :: system_clock时钟;
typedef Clock :: time_point TimePoint;

我们的 main 看起来像

  int main(int argc,char * argv [])
{
// argc
const Clock :: rep d = static_cast< Clock :: rep>(std :: atoi(argv [1]));
//现在
TimePoint now = Clock :: now();
//从0开始
auto days = std :: chrono :: duration< Clock :: rep,Ratios :: Days> :: zero();

//现在我们要添加d到天
天+ = d; //错误!
days.count()= d; //错误!
天=天+ d; //错误!
days + = std :: chrono :: duration< Clock :: rep,Ratios :: Days>(d); // Okay
days = days + std :: chrono :: duration< Clock :: rep,Ratios :: Days>(d); // Okay

days * = d; //为什么这么好?
days%= d; //这也是吗?

时间点后=现在+天;

return 0;
}

禁止用户操作 duration 直接?

解决方案

这样做是为了强制你坚持强类型值,任何值。



Bjarne Stroustrup在C ++编程语言(第4版,35.2.1,第1011页)中提供了关于此行为的示例:







期间是一个单位系统,因此没有 = 或 + = 采用一个简单的值。允许添加 5



  duration< long long,milli> d1 {7}; // 7 milliseconds 
d1 + = 5; // error
[...]
/ pre>

,例如:




  d1 + = duration< long long,milli> {5}; // OK:milliseconds





Suppose we have

#include <chrono>
#include <iostream>
#include <ctime>

namespace Ratios { typedef std::ratio<60*60*24,1> Days; }

typedef std::chrono::system_clock Clock;
typedef Clock::time_point TimePoint;

And our main looks like

int main(int argc, char *argv[])
{
    // argc check left out for brevity
    const Clock::rep d = static_cast<Clock::rep>(std::atoi(argv[1]));
    // Right now
    TimePoint now = Clock::now();
    // Start with zero days
    auto days = std::chrono::duration<Clock::rep, Ratios::Days>::zero();

    // Now we'd like to add d to the days
    days += d; // Error!
    days.count() = d; // Error!
    days = days + d; // Error!
    days += std::chrono::duration<Clock::rep, Ratios::Days>(d); // Okay
    days = days + std::chrono::duration<Clock::rep, Ratios::Days>(d); // Okay

    days *= d; // Why is this okay?
    days %= d; // And this too?

    TimePoint later = now + days;

    return 0;
}

What is the reason behind prohibiting the user to manipulate a duration directly?

解决方案

It is done to enforce you to stick to strongly typed values rather than arbitrary values.

Bjarne Stroustrup has examples regarding this behaviour in "The C++ Programming Language" (4th Ed., 35.2.1, pp. 1011):


"The period is a unit system, so there is no = or += taking a plain value. Allowing that would be like allowing the addition of 5 of an unknown SI unit to a length in meters. Consider:

duration<long long, milli> d1{7}; // 7 milliseconds
d1 += 5; // error
[...]

What would 5 mean here? 5 seconds? 5 milliseconds? [...] If you know what you mean, be explicit about it. For example:

d1 += duration<long long, milli>{5}; //OK: milliseconds"


这篇关于什么是std :: chrono :: duration缺乏立即计数操作的背后的原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆