c++ - Accepting std::chrono::duration of any representation/period -
i want function accept duration in whatever units makes sense caller.
for example:
transition->after(chrono::seconds(3)); transition->after(chrono::milliseconds(500)); transition->after(chrono::hours(1)); what valid signature after function like? can avoid making templated function?
there handful of common options
1) use template. has advantage there no conversion, requires use of template, might not possible. example, if interface virtual function.
template<typename rep, typename period> void after( std::chrono::duration< rep, period > t); 2) use integer representation, , lowest period interface needs. example, if function realistically needs nanoseconds, use directly. implicitly convert nanoseconds, if no loss of precision occur. can specify other periods using pre-defined ones, or explicitly specifying part of duration
void after( std::chrono::nanoseconds t) ; 3) use double representation, can useful if precision isn't issue, ability accept inputs is. implicitly convert duration, since conversion floating point type allowed periods. example, if wanted double-precision seconds, do
void after( std::chrono::duration< double > t);
Comments
Post a Comment