How to handle units in c++ interface

前端 未结 9 2140
独厮守ぢ
独厮守ぢ 2021-02-07 02:56

I am currently designing an API where I want that the user to be able to write code like this:

PowerMeter.forceVoltage(1 mV);
PowerMeter.settlingTime(1 ms);
         


        
9条回答
  •  灰色年华
    2021-02-07 03:30

    You could use C++11's compile-time rational arithmetic support for the units, instead of defining literals or macros for the units.

提交回复
热议问题