如何使用boost :: units添加自己的基本单元和转换

Dea*_*vey 9 c++ boost boost-units

我目前正在使用boost :: units来表示si单位的扭矩,但是我给出了英尺扭矩.我试图创造一个pound_foot单位的扭矩和转换来支持这一点.我的懒惰尝试只是简单地定义:

BOOST_STATIC_CONST(boost::si::torque, pound_feet = 1.3558179483314 * si::newton_meters);
Run Code Online (Sandbox Code Playgroud)

然后做:

boost::si::torque torque = some_value * pound_feet;
Run Code Online (Sandbox Code Playgroud)

但这感觉不尽如人意.我的第二次尝试是尝试定义一个名为pound_foot的新基本单元(见下文).但是当我尝试以类似于上面的方式使用它时(使用转换为si单元),我得到一个充满错误的页面.有关正确方法的任何建议吗?

namespace us {
  struct pound_foot_base_unit : base_unit<pound_foot_base_unit, torque_dimension> { };
    typedef units::make_system<
            pound_foot_base_unit>::type us_system;
    typedef unit<torque_dimension, us_system> torque;
    BOOST_UNITS_STATIC_CONSTANT(pound_foot, torque);
    BOOST_UNITS_STATIC_CONSTANT(pound_feet, torque);        
}
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(us::torque, 
                                     boost::units::si::torque, 
                                     double, 1.3558179483314);
Run Code Online (Sandbox Code Playgroud)

thi*_*ton 7

磅脚不是真正的基本单位,所以最好采用干净的方式并根据基本单位(英尺和英尺)来定义:

#include <boost/units/base_units/us/pound_force.hpp>
#include <boost/units/base_units/us/foot.hpp>
#include <boost/units/systems/si/torque.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/io.hpp>
#include <iostream>

namespace boost {
namespace units {
namespace us {

typedef make_system< foot_base_unit, pound_force_base_unit >::type system;
typedef unit< torque_dimension, system > torque;

BOOST_UNITS_STATIC_CONSTANT(pound_feet,torque);

}
}
}

using namespace boost::units;

int main() {
    quantity< us::torque > colonial_measurement( 1.0 * us::pound_feet );
    std::cerr << quantity< si::torque >(colonial_measurement) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序从已知的英尺和英尺值计算转换因子,输出为1.35582平方公尺s ^ -2rad ^ -1.但请允许我嘲笑帝国制度的劣势.