【问题标题】:Boost::units with non SI unitsBoost::units 非 SI 单位
【发布时间】:2021-02-25 15:04:45
【问题描述】:

这让我发疯了。我正在尝试做一些简单的事情:在 si::meter 和 US foot 之间转换。

我能做到:

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

但那里似乎不是基于美国的系统,例如我找不到

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

正确吗?美国基本单位的预期用途是什么?

编辑澄清:所有基于美国的单位都已定义,但我不知道如何正确使用它们,并且缺少文档。

【问题讨论】:

    标签: c++ boost type-conversion boost-units


    【解决方案1】:

    看了很多radar_beam_height.cpp的例子后,我想出了以下解决方案:

    #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;
    }
    

    输出如预期:

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

    有人可以确认这是预期用途吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      相关资源
      最近更新 更多