How to handle units in c++ interface

前端 未结 9 2118
独厮守ぢ
独厮守ぢ 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:43

    You can see the library "C++ Units" from Calum Grant as a good example of how to implement this. The library is a bit outdated, but still worth to see or may be to use.

    Also, i think it might be interesting to read: "Applied Template Metaprogramming in SI UNITS: the Library of Unit-Based Computation"

    There is one more good library: UDUNITS-2 which:

    contains a C library for units of physical quantities and a unit-definition and value-conversion utility.

提交回复
热议问题