c++boostboost-units

conversion factor for units in boost.units


I just started using boost-units, and I am trying to understand what code I should write to get the conversion factor between units. Following the Runtime Units Example, I managed to get what I need for base_units.

As an example, for length I can easily get the conversion factor from inch to meter as follows:

conversion_factor(imperial::inch_base_unit::unit_type(), si::meter_base_unit::unit_type())

This eventually allows me to define as many scaled units as I want, and get all the needed conversion factors.

In the case of velocity, for example, which is declared as 'meter_per_second' unit in the relevant header, I cant figure out how to retrieve the conversion factor to convert it to kmh, or mph. I guess I need to define my own mph unit, which leads to the need to define (or retrieve) the definition of miles and hours, and putting them all together.

How am I supposed achieve the desired result?


Solution

  • I managed to find a relevant example in the Boost-users mailing list archive (here)

    #include <boost/units/base_units/us/mile.hpp>
    #include <boost/units/base_units/metric/hour.hpp>
    
    typedef boost::units::us::mile_base_unit::unit_type mile_unit;
    typedef boost::units::metric::hour_base_unit::unit_type hour_unit;
    
    typedef boost::units::divide_typeof_helper<mile_unit, hour_unit>::type miles_per_hour;
    

    Once the unit is correctly declared, the conversion_factor function can be called to get what I need.