Casting Effort :: Unit :: Double Quantity

I need to pass the quantity value to the library for evaluation. The boost block library accepts double values ​​in SI, so the boost block library is very attractive for this requirement. However, how do I pass a quantity to a double value? The documentation and example seem to avoid this, as the goal is, rightfully, to support units.

Sort of:

quantity<pressure> p(101.1 * kilo * pascals); double dblP = static_cast<double>(p); // double value in Pascals 

Passing the headers suggests ... Is this the right way to cast into a base type?

 p.value(); 
+6
source share
3 answers

The reference documentation shows that implicit casts or the member method value() can be used.

  • operator value_type() const;

    implicit conversion to value_type allowed

  • const value_type & value() const;

    constant access to value

+3
source

I think you are looking for:

 quantity<pressure> p(101.1 * kilo * pascals); double dblP = p / pascals; // double value in Pascals 

If you split the block, you are left with quantity<dimensionless> , which will be implicitly converted to double . This eliminates any question about what the internal representation (which value() returns) of units.

+8
source

Just noticed it. I think the intended method is to use the Boost quantity_cast operator.

 quantity<pressure> p(101.1 * kilo * pascals); double dblP = quantity_cast<double>(p); 

http://www.boost.org/doc/libs/1_55_0/doc/html/boost_units/Quantities.html#boost_units.Quantities.Quantity_Construction_and_Conversion

+3
source

All Articles