Get an unsigned int milliseconds out of chrono::duration

前端 未结 2 1671
忘掉有多难
忘掉有多难 2021-02-07 11:17

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

bool setTimer(std::chrono::duration

        
2条回答
  •  盖世英雄少女心
    2021-02-07 11:52

    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';
    }
    

    online demo: http://coliru.stacked-crooked.com/a/03f29d41e9bd260c

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

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

提交回复
热议问题