1

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?

BaroneAshura
  • 191
  • 1
  • 1
  • 14
  • I'm not so sure about that linked sample. It seems to return wrong things: http://coliru.stacked-crooked.com/a/615e8be190e18463 – sehe Sep 20 '17 at 20:45
  • Oh aha. That sample is not doing what you'd think it does: https://www.wolframalpha.com/input/?i=sqrt((1+foot)+%5E+2+%2B+(2m)%5E2) – sehe Sep 20 '17 at 20:54
  • @sehe the linked example is relevant only for the use of the **conversion_factor** function, which seems to return what I need. I have compiled nor run the code actually. – BaroneAshura Sep 21 '17 at 06:45

1 Answers1

1

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.

BaroneAshura
  • 191
  • 1
  • 1
  • 14
  • Using C++11 you can write it as [`using miles_per_hour = decltype(mile_unit{}/hour_unit{});` -- see live demo/proof](http://coliru.stacked-crooked.com/a/55b960720b737c48) – sehe Sep 21 '17 at 07:32
  • @sehe thanks for the suggestion, I will investigate if I can do it, since the source code needs to be compiled with different compilers, and at the moment it is unclear to me which C++ standards we can take advantage of (at the moment we stick to C++98 -.-') – BaroneAshura Sep 21 '17 at 08:39