Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Construction and Conversion of Quantities

This library is designed to emphasize safety above convenience when performing operations with dimensioned quantities. Specifically, construction of quantities is required to fully specify both value and unit. Direct construction from a scalar value is prohibited (though the static member function from_value is provided to enable this functionality where it is necessary. In addition, a quantity_cast to a reference allows direct access to the underlying value of a quantity variable. An explicit constructor is provided to enable conversion between dimensionally compatible quantities in different unit systems. Implicit conversions between unit systems are allowed only when the reduced units are identical, allowing, for example, trivial conversions between equivalent units in different systems (such as SI seconds and CGS seconds) while simultaneously enabling unintentional unit system mismatches to be caught at compile time and preventing potential loss of precision and performance overhead from unintended conversions. Assignment follows the same rules. An exception is made for quantities for which the unit reduces to dimensionless; in this case, implicit conversion to the underlying value type is allowed via class template specialization. Quantities of different value types are implicitly convertible only if the value types are themselves implicitly convertible. The quantity class also defines a value() member for directly accessing the underlying value.

To summarize, conversions are allowed under the following conditions :

Of course, any time implicit conversion is allowed, an explicit conversion is also legal.

Because dimensionless quantities have no associated units, they behave as normal scalars, and allow implicit conversion to and from the underlying value type or types that are convertible to/from that value type.


PrevUpHomeNext