Boost logo

Boost Users :

Subject: Re: [Boost-users] [units] Creating specific unit quantities
From: Matthias Schabel (boost_at_[hidden])
Date: 2011-03-07 17:35:03


> On Thu, Mar 3, 2011 at 4:58 PM, Matthias Schabel <boost_at_[hidden]> wrote:
>
> Why don't you give a code snippet that does what you want to do?
>
> using namespace boost::units;
>
> class Test {
> public:
> void setX(quantity<si::length> const& x) {
> m_X = x;
> }
> private:
> quantity<si::length> m_X;
> };
>
> int main(int, char const *[]) {
>
> typedef boost::units::us::foot_base::unit_type foot_unit;
> static const foot_unit foot;
>
> Test a;
>
> quantity<si::length> first (1.0 * si::meter);
> quantity<foot_unit> second(1.0 * foot);
> quantity<boost::units::si::meter_base_unit::unit_type> third (1.0 * si::meter);
>
> a.setX(first);
> a.setX(second);
> a.setX(third);
>
> return 0;
> }

#include <boost/units/quantity.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/base_units/us/foot.hpp>

using namespace boost::units;
 
class Test {
  public:
    // takes any unit of length
    template<class System>
    void setX(quantity<unit<length_dimension,System> > const& x) {
      // explicit conversion to si::length = meters
      m_X = quantity<si::length>(x);
    }
  private:
    quantity<si::length> m_X;
};
 
int main(int, char const *[]) {
 
typedef boost::units::us::foot_base_unit::unit_type foot_unit;
static const foot_unit foot;
 
Test a;
 
quantity<si::length> first (1.0 * si::meter);
quantity<foot_unit> second(1.0 * foot);
quantity<boost::units::si::meter_base_unit::unit_type> third (1.0 * si::meter);
 
a.setX(first);
a.setX(second);
a.setX(third);
 
return 0;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net