dimanche 31 mai 2015

How to use `duration_cast` for a derived class?

I'm cleaning up my timer class using std::chrono. Everything else went smooth except that I cannot seem to apply duration_cast to derived classes. Well, I made it work in a different way, but I'd still like to know what I missed.

See here for the error message.

#include <iostream>
#include <chrono>

typedef std::chrono::high_resolution_clock Clock;

class Milliseconds : public std::chrono::milliseconds
{
public:
    typedef std::chrono::milliseconds Base;
    typedef Base::rep Type;

    using Base::Base;
};

inline Milliseconds::Type millisecondsSinceEpoch()
{
    return std::chrono::duration_cast<Milliseconds::Base>(Clock::now().time_since_epoch()).count();
    //duration_cast<Milliseconds> ERROR!
}

int main() {
    using namespace std;
    cout << millisecondsSinceEpoch() << endl;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire