Boost logo

Boost :

Subject: Re: [boost] [units] problems
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2013-09-16 15:53:21


Hi Robert,

Robert Ramey wrote:

> it looks like I'm missing something; Here is the test program again - with
> all the headers.
>
> #include <boost/units/systems/si/length.hpp>
> #include <boost/units/base_units/us/mile.hpp>
> #include <boost/units/quantity.hpp>
> int main(int argc, char * argv[]){
> using namespace boost::units;
> using namespace boost::units::si;
> using namespace boost::units::us;
> quantity<length, float> l1;
> l1 = 1.0 * meters;
> quantity<length, float> l2;
> l2 = static_cast<quantity<length, float> >(1.0 * miles);
> quantity<length, float> l3 = l1 + l2;
> return 0;
> }
>

Just in case you haven't gotten this yet, here's something that compiles
and runs (with g++):
#include <boost/units/io.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/static_constant.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/base_units/us/mile.hpp>
#include <iostream>

namespace boost { namespace units { namespace us
{
  typedef boost::units::make_system<boost::units::us::mile_base_unit>::type
mile_system;
  typedef boost::units::unit<boost::units::length_dimension,mile_system>
mile_length;
  BOOST_UNITS_STATIC_CONSTANT(mile, mile_length);
  BOOST_UNITS_STATIC_CONSTANT(miles, mile_length);
}}}

int main(){
    using namespace boost::units;
    using namespace boost::units::si;
    using namespace boost::units::us;

    quantity<length, float> l1;
    l1 = 1000.f * meters;
    quantity<length, float> l2;
    l2 = static_cast<quantity<length, float> >(1.f * miles);
    quantity<length, float> l3 = l1 + l2;

    std::cout << l3 << std::endl;
}
//Outputs 2609.34 m

The key is that value times unit = quantity, but you can't create a unit
from a base_unit without a system. (There doesn't appear to be a system
which declares mile units definitions, as you already found, I think.)
Unfortunately, any other base_units which aren't used in the systems folder
will require similar boilerplate.

HTH,
Nate


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk