Search code examples
c++function-pointerstypedef

Typedef, or alias, or something for std::chrono::duration_cast for easier typing


This is, most probably, a simple question, but I don't know how to do it. How can std::chrono::duration_cast<std::chrono::microseconds>() be used with typedef, or std::function, or anything else? I can't use using std::chrono, I tried to make a pointer to a function, but when I looked over cppreference, I get dizzy, plus all the codes I see use auto, which doesn't help.


Solution

  • There are many ways to do it. it depends what exactly you are trying to achieve.

    One way to approach this is define own helper function (in own namespace or global):

    template <typename T>
    std::chrono::microseconds asMicroseconds(T duration)
    {
        return std::chrono::duration_cast<std::chrono::microseconds>(duration);
    }