Boost logo

Boost Users :

Subject: Re: [Boost-users] [units] runtime conversion approach
From: Matthias Schabel (boost_at_[hidden])
Date: 2011-07-29 12:37:09

> I need some help understanding unit conversions using boost::units. I see there are conversions.hpp header files. Does this mean that there are built-in unit conversions for fundamental units, or even mainstream derived ones by simply including the conversions and performing the appropriate operation?
> Can I do something like this, for instance?
> quantity<si::length> test_m = 1.0 * meter;
> quantity<us::inch> test_in = test_m * inch / meter;

You need to do conversions explicitly, so

#include <iosfwd>
#include <iostream>

#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/base_units/us/inch.hpp>

using namespace boost::units;

int main()
        typedef us::inch_base_unit::unit_type inch_unit;
        quantity<si::length> test_m(1.0*si::meter);
        quantity<inch_unit> test_in(test_m);
        std::cout << test_m << "\t" << test_in << std::endl;

        return 0;


1 m 39.3701 in

> What is the best way to go about setting up conversions, or for that matter, defining a unit system; for instance, the us (english) unit system does not appear to be available?

As long as conversions between the base units you are using are defined, the conversions should just work. Study libs/units/examples/radar_beam_height.cpp - that should make it clearer...


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at