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<class Y>
quantity<si::amount,Y>
idealGasLaw(const quantity<si::pressure,Y>& P,
const quantity<si::volume,Y>& V,
const quantity<si::temperature,Y>& 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<temperature> T = (273.+37.)*kelvin;
quantity<pressure> P = 1.01325e5*pascals;
quantity<length> r = 0.5e-6*meters;
quantity<volume> V = (4.0/3.0)*3.141592*pow<3>(r);
quantity<amount> 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