2

我想boost::units在项目中使用a来使用尺寸分析和单位系统之间的自动转换。我想用标准工程单位表示代码中的数量,这些单位通常是其他单位的缩放版本。让我用一个例子来解释这一点。假设我定义了以下系统

typedef make_system<
    us::foot_base_unit,
    us::pound_base_unit,
    si::second_base_unit>::type my_system;

BOOST_UNITS_STATIC_CONSTANT(feet,length);

BOOST_UNITS_STATIC_CONSTANT(pound,mass);

然后长度单位将定义为英尺,力 inlb*ft*s^-2和压力 in lb*ft^-1*s^-2。但是,我想使用磅力单位的力和 PSI 中的压力,即每平方英寸的磅力。我以为我可以使用缩放单位来表达这些并可以互换使用它们,但事实似乎并非如此。

我试过这个:

struct poundforcescale {
    typedef double value_type;
    double value() const { return 32.174049; }
};

typedef make_scaled_unit<force, poundforcescale>::type poundForce;
namespace boost {

  namespace units {
    std::string name_string(const field::poundForce&) { return "pound-force"; }
    std::string symbol_string(const field::poundForce&) { return "lbf"; }
 }
}

哪个编译没有问题。但是当我尝试像这样使用缩放单位时:

poundForce poundForceInst;
quantity<field::poundForce> f2 = 1*poundForceInst;
quantity<field::force> f3 = f2;

编译失败,出现“没有可行的转换错误”。我认为缩放单位的目的是自动进行这些转换。而且文档也让我认为我只需要定义 name_string 和 symbol_string 就可以打印磅力数量,但是这个

std::cout << "f2 = " << f2 << std::endl;

导致“boost::units::scale_list_dim 中没有名为符号的成员”错误。显然重载这些函数不适用于缩放单位。

也许我应该定义另一个这样的系统

typedef make_system<
    us::foot_base_unit,
    us::pound_force_base_unit,
    si::second_base_unit>::type my_system;

但是,如果我想用 ft 表示长度和用 psi 表示压力,我无论如何都需要转换。

如果有人有更好的解决方案,我会很高兴。

4

0 回答 0