c++c++11metaprogrammingstdrational-numbers

Ratio<,> is constant, but what if I want to accept different ratios as an argument?


Perhaps an oxymoronic question: ratio<,> is, by definition, a compile-time constant.

However, I would like to construct durations with different ratios that can be specified by the caller of my method. I'm guessing I should be using something other than ratio and/or duration, then, but what?

I want to have, say, a class member that can be set at runtime, and I would like it to be of type ratio<,>. At some point in the code, where this member gets set/assigned, it would be assigned a constant ratio, but in my class, I don't want to specify what that ratio should be.


Solution

  • Ok, you need a std::ratio which "can be specified by the caller". There are three different options: