One of the BoostLibraries
A brilliant example of the power of Static Typing. An extensible framework for compile-time dimensional analysis in C++ with no runtime overhead.
Example:
/// the ideal gas law in si units
template
quantity
idealGasLaw(const quantity& P,
const quantity& V,
const quantity& T)
{
using namespace boost::units::si;
#if BOOST_UNITS_HAS_TYPEOF
using namespace constants::codata;
return (P*V/(R*T));
#else
return P*V/(8.314472*(joules/(kelvin*mole))*T);
#endif // BOOST_UNITS_HAS_TYPEOF
}
Which is used as follows:
/// test ideal gas law
quantity T = (273.+37.)*kelvin;
quantity P = 1.01325e5*pascals;
quantity r = 0.5e-6*meters;
quantity V = (4.0/3.0)*3.141592*pow<3>(r);
quantity n(idealGasLaw(P,V,T));
Note that defining the base units is sufficient to use any plausible derived unit.
See also StaticTypeSafety
CategoryBoost CategoryCpp CategoryCppTemplates CategoryMetaprogramming