If I just want to convert a value from one unit to another, what's the simplest (ideally one-line) way of doing this?
For instance, i want to store a value in meters, but specify it in miles.
Most examples for doing this seem to be many lines long, involve typedefs and unit definitions, and don't give a simple unitless output.
There are two ways of doing this quickly and easily.
Say you want to convert 100 miles to metres...
The first explicitly constructs a quantity:
#include <boost/units/quantity.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/base_units/imperial/mile.hpp>
double distance_in_metres = boost::units::quantity<boost::units::si::length>(
100.0 * boost::units::imperial::mile_base_unit::unit_type()
) / boost::units::si::meter;
The second creates a conversion factor and multiplies by that:
#include <boost/units/systems/si/length.hpp>
#include <boost/units/base_units/imperial/mile.hpp>
double distance_in_metres = 100.0 *
boost::units::conversion_factor(
boost::units::imperial::mile_base_unit::unit_type(),
boost::units::si::meter
);