获得chrono :: duration的无符号整数毫秒 [英] Get an unsigned int milliseconds out of chrono::duration

查看:335
本文介绍了获得chrono :: duration的无符号整数毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Winapi包装器,我想在给定调用持续时间内使用chrono。代码示例:

For a winapi wrapper I want to use chrono for a duration given to the call. The code example:

bool setTimer(std::chrono::duration<std::chrono::milliseconds> duration)
{
    unsigned int dwDuration = Do some chrono magic here

    SetTimer(m_hWnd,1,dwDuration,0);
}

dw持续时间必须以毫秒为单位。

dwDuration has to be in milliseconds.

第一个问题:如何做魔术。

First question: How do to the magic.

第二个问题:参数声明可以吗?

Second question: Is the parameter declaration okay?

推荐答案

类型名称为 std :: chrono :: milliseconds ,它的成员函数为 count()返回毫秒数:

The name of the type is std::chrono::milliseconds, and it has a member function count() that returns the number of those milliseconds:

bool setTimer(std::chrono::milliseconds duration)
{
    unsigned int dwDuration = duration.count();
    return std::cout << "dwDuration = " << dwDuration << '\n';
}

在线演示:http://coliru.stacked-crooked.com/a/03f29d41e9bd260c

如果您想成为超级- pedantic,count()的返回类型为 std :: chrono :: milliseconds :: rep

If you want to be ultra-pedantic, the return type of count() is std::chrono::milliseconds::rep

要处理小数毫秒,则类型将为 std :: chrono :: duration< double,std :: milli> (count()的返回类型为然后加倍)

If you want to deal with fractional milliseconds, then the type would be std::chrono::duration<double, std::milli> (and the return type of count() is then double)

这篇关于获得chrono :: duration的无符号整数毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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