c++boosttype-conversionboost-units

Boost::units with non SI units


This is driving me nuts. I'm trying to do something simple: Convert between si::meter and US foot.

I can do do:

quantity<si::length> l = 1 * si::meters;

but there doesn't seem to be a US based system, e.g. I cannot find

quantity<si::length> l = 1 * us::foot;

Is that correct? What is the intended use of the US base units?

Edit to clarify: All the US based units are defined, but I can't figure out how to properly use them and the documentation is lacking.


Solution

  • After a lot of staring at the radar_beam_height.cpp example, I came up with the following solution:

    #include <boost/units/base_units/us/foot.hpp>
    #include <boost/units/io.hpp>
    #include <boost/units/quantity.hpp>
    #include <boost/units/systems/si/length.hpp>
    #include <iostream>
    
    namespace us {
    typedef boost::units::make_system<boost::units::us::foot_base_unit>::type system;
    typedef boost::units::unit<boost::units::length_dimension, system> length;
    static const length foot, feet;
    } // namespace us
    
    using namespace boost::units;
    
    int main() {
        quantity<si::length> l0 = 1 * si::meters;
        quantity<si::length> l1{1 * ::us::foot};
        quantity<::us::length> l2 = 1 * ::us::foot;
    
        std::cout << "l0 = " << l0 << std::endl;
        std::cout << "l1 = " << l1 << std::endl;
        std::cout << "l2 = " << l2 << std::endl;
    
        return 0;
    }
    

    The output is as expected:

    l0 = 1 m
    l1 = 0.3048 m
    l2 = 1 ft
    

    Can someone confirm that this is the intended use?