2

I would like to use a boost::units in a project to use dimensional analysis and automatic conversions between unit systems. I would like to express quantities in the code with standard engineering units, that are often scaled versions of other units. Let me explain this with an example. Suppose I define the following system

typedef make_system<
    us::foot_base_unit,
    us::pound_base_unit,
    si::second_base_unit>::type my_system;

BOOST_UNITS_STATIC_CONSTANT(feet,length);

BOOST_UNITS_STATIC_CONSTANT(pound,mass);

Then length units would be defined in feet, force in lb*ft*s^-2 and pressure in lb*ft^-1*s^-2. However, I would like to use force in pound-force units and pressure in PSI which are pound-force per square inch. I thought I could use scaled units to express these and use them interchangeably but this doesn't seem to be the case.

I tried this:

struct poundforcescale {
    typedef double value_type;
    double value() const { return 32.174049; }
};

typedef make_scaled_unit<force, poundforcescale>::type poundForce;
namespace boost {

  namespace units {
    std::string name_string(const field::poundForce&) { return "pound-force"; }
    std::string symbol_string(const field::poundForce&) { return "lbf"; }
 }
}

Which compiled without a problem. But then When I tried to use the scaled unit like this:

poundForce poundForceInst;
quantity<field::poundForce> f2 = 1*poundForceInst;
quantity<field::force> f3 = f2;

The compilation failed with an "no viable conversion error". I thought that the point of scaled units was to make these conversions automatically. And also the documentation made me think that I only needed to define name_string and symbol_string to be able to print the pound-force quantities, but this

std::cout << "f2 = " << f2 << std::endl;

resulted in a "no member named symbol in boost::units::scale_list_dim" error. Apparently overloading these functions doesn't work for scaled units.

Maybe I should define another system like this

typedef make_system<
    us::foot_base_unit,
    us::pound_force_base_unit,
    si::second_base_unit>::type my_system;

But I would need conversions anyway if I want to express length in ft and pressure in psi.

I would be glad if someone has a better solution.

quantdev
  • 23,517
  • 5
  • 55
  • 88
maxdebayser
  • 1,066
  • 7
  • 10
  • I figured out that quantity f3(1*poundForceInst); works, but the conversion attribution should work too, as the manual states: 1) explicit conversion between quantity and quantity is allowed if Unit1 and Unit2 have the same dimensions and if Y and Z are implicitly convertible. 2) implicit conversion between quantity and quantity is allowed if Unit1 reduces to exactly the same combination of base units as Unit2 and if Y and Z are convertible. – maxdebayser Aug 13 '14 at 21:09
  • Did you solve the custom name and symbol compilation problem? I have a very similar situation and I can't get the symbol_string overload to work... – Dina Dec 08 '14 at 11:56

0 Answers0