c++ - 如何使用 boost::units 添加你自己的基本单位和转换

标签 c++ boost boost-units

我目前使用 boost::units 来表示以 si 单位表示的扭矩,但是我得到的是以磅英尺为单位的扭矩。因此,我试图创建一个磅英尺的扭矩单位和一个转换来支持它。我懒惰的尝试是简单地定义:

BOOST_STATIC_CONST(boost::si::torque, pound_feet = 1.3558179483314 * si::newton_meters);

然后做:

boost::si::torque torque = some_value * pound_feet;

但这感觉并不令人满意。我的第二次尝试是尝试定义一个名为 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);

最佳答案

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

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

此程序根据已知的英尺和磅值计算换算系数,输出为 1.35582 m^2 kg s^-2 rad^-1。尽管如此,请允许我对帝制的劣势嗤之以鼻。

关于c++ - 如何使用 boost::units 添加你自己的基本单位和转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7640079/

相关文章:

c++ - 我可以从 divide_typeof_helper 检索基本单位吗?

c++ - 如何在 Boost Unit 中正确定义派生单位

c++ - #在头文件或实现文件中导入

c++ - Q3DBars 在垂直墙上有网格,怎么样?

c++ - boost::hash包含元组的元组

boost - 如何将动态任务设置为defaultTask

c++ - 链接 boost.asio

c++ - 在 Eclipse CDT (Juno/Kepler/Luna) 索引器中启用 C++11

c++ - std::strings 的容量()、保留()和调整大小()函数

c++ - Boost.序列化: Error on Calling Class Method during serialization process