c++ - How to use `duration_cast` for a derived class? -
i'm cleaning timer class using std::chrono
. else went smooth except cannot seem apply duration_cast
derived classes. well, made work in different way, i'd still know missed.
see here 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; }
the standard duration_cast
can used convert specialization of duration
. [time.duration.cast]/p1:
template <class toduration, class rep, class period> constexpr toduration duration_cast(const duration<rep, period>& d);
remarks: function shall not participate in overload resolution unless
toduration
instantiation ofduration
.
if toduration
not specialization of duration
, signature removed overload resolution (by sfinae or similar technique), , call fails unless function template called duration_cast
available.
Comments
Post a Comment