![]() |
Home | Libraries | People | FAQ | More |
This example demonstrates the various allowed conversions between SI and CGS units. Defining some quantities
quantity<si::length> L1 = quantity<si::length,int>(int(2.5)*si::meters); quantity<si::length,int> L2(quantity<si::length,double>(2.5*si::meters));
illustrates implicit conversion of quantities of different value types where implicit conversion of the value types themselves is allowed. N.B. The conversion from double to int is treated as an explicit conversion because there is no way to emulate the exact behavior of the built-in conversion. Explicit constructors allow conversions for two cases:
quantity
to a different value_type
:
quantity<si::length,int> L3 = static_cast<quantity<si::length,int> >(L1);
quantity
to a different unit :
quantity<cgs::length> L4 = static_cast<quantity<cgs::length> >(L1);
giving the following output :
L1 = 2 m L2 = 2 m L3 = 2 m L4 = 200 cm L5 = 5 m L6 = 4 m L7 = 200 cm
A few more explicit unit system conversions :
quantity<si::volume> vs(1.0*pow<3>(si::meter)); quantity<cgs::volume> vc(vs); quantity<si::volume> vs2(vc); quantity<si::energy> es(1.0*si::joule); quantity<cgs::energy> ec(es); quantity<si::energy> es2(ec); quantity<si::velocity> v1 = 2.0*si::meters/si::second, v2(2.0*cgs::centimeters/cgs::second);
which produces the following output:
volume (m^3) = 1 m^3 volume (cm^3) = 1e+06 cm^3 volume (m^3) = 1 m^3 energy (joules) = 1 J energy (ergs) = 1e+07 erg energy (joules) = 1 J velocity (2 m/s) = 2 m s^-1 velocity (2 cm/s) = 0.02 m s^-1