Millimeters in boost::units

后端 未结 3 1496
鱼传尺愫
鱼传尺愫 2021-01-05 09:55

I want to use boost::units for some SI metrics. However our code mostly deals with millimeters and instead of using

quantity value = 1*milli*m         


        
3条回答
  •  不知归路
    2021-01-05 10:50

    I am using the following approach:

    // your namespace name for units
    namespace outernamespace {
        namespace millimeter_system {
            typedef boost::units::scaled_base_unit>> millimeter_base_unit;
    
            typedef boost::units::make_system::type system;
            typedef boost::units::unit length;
    
            BOOST_UNITS_STATIC_CONSTANT(millimeter, length);
            BOOST_UNITS_STATIC_CONSTANT(millimeters, length);
        }
    
        typedef boost::units::quantity quantity_millimeter;
        using millimeter_system::millimeter;
        using millimeter_system::millimeters;
    }
    
    // demonstration of usage
    void foo() {
        using namespace outernamespace;
        using namespace boost::units;
        using namespace boost::units::si;
    
        quantity_millimeter mm = 5 * millimeter;
        quantity m = 0.004 * meter;
        if (mm < static_cast(m)) {
            std::cout << 'lt ' << std::endl;
        }
        else {
            std::cout << 'geq ' << std::endl;
        }
    }
    

提交回复
热议问题