如何将 Boost Units 中的缩放单位转换为字符串

huangapple go评论60阅读模式
英文:

How to make scaled units in Boost Units convertable to string

问题

I want to define the unit Watt hours which has the same dimensions as Joule but needs a conversion factor of 3600. Therefore, I want to define it as a scaled unit.

I've defined my unit as follows:

namespace si = boost::units::si;
typedef boost::units::make_scaled_unit<si::energy, boost::units::scale<3600, boost::units::static_rational<1>>>::type watt_hour_unit;
BOOST_UNITS_STATIC_CONSTANT(watt_hours, watt_hour_unit);
typedef boost::units::quantity<watt_hour_unit> watt_hour;

I can use it without problems:

watt_hour const energy_whs = 1.0 * watt_hours;
boost::units::quantity<boost::units::si::energy, double> energy_conv(energy_whs);
std::cout << energy_conv << std::endl; // "3600 J"

But when I try to print it, I get an error:

std::cout << energy_whs << std::endl; // should be "1 Wh"

yields these errors

[...]
/usr/include/boost/units/io.hpp:327:39: error: symbol is not a member of boost::units::list<boost::units::scale_list_dim<boost::units::scale<3600, boost::units::static_rational<1> > >, boost::units::dimensionless_type>::item {aka boost::units::scale_list_dim<boost::units::scale<3600, boost::units::static_rational<1> > >}
  327 |             str += Begin::item::symbol();
      |                    ~~~~~~~~~~~~~~~~~~~^~
[...]
/usr/include/boost/units/io.hpp:393:37: error: name is not a member of boost::units::list<boost::units::scale_list_dim<boost::units::scale<3600, boost::units::static_rational<1> > >, boost::units::dimensionless_type>::item {aka boost::units::scale_list_dim<boost::units::scale<3600, boost::units::static_rational<1> > >}
  393 |             str += Begin::item::name();
      |                    ~~~~~~~~~~~~~~~~~^~

I tried defining the name_string and symbol_string which makes the second error disappear but not the first.

namespace boost
{
namespace units
{
std::string name_string(const watt_hour_unit&)
{
    return "Watt hours";
}

std::string symbol_string(const watt_hour_unit&)
{
    return "Wh";
}
}
}
英文:

I want to define the unit Watt hours which has the same dimensions as Joule but needs a conversion factor of 3600. Therefore, I want to define it as a scaled unit.

I've defined my unit as follows:

namespace si = boost::units::si;
typedef boost::units::make_scaled_unit&lt;si::energy, boost::units::scale&lt;3600, boost::units::static_rational&lt;1&gt;&gt;&gt;::type watt_hour_unit;
BOOST_UNITS_STATIC_CONSTANT(watt_hours, watt_hour_unit);
typedef boost::units::quantity&lt;watt_hour_unit&gt; watt_hour;

I can use it without problems:

watt_hour const energy_whs = 1.0 * watt_hours;
boost::units::quantity&lt;boost::units::si::energy, double&gt; energy_conv(energy_whs);
std::cout &lt;&lt; energy_conv &lt;&lt; std::endl; // &quot;3600 J&quot;

But when I try to print it, I get an error:

std::cout &lt;&lt; energy_whs &lt;&lt; std::endl; // should be &quot;1 Wh&quot;

yields these errors

[...]
/usr/include/boost/units/io.hpp:327:39: error: ‘symbol’ is not a member of ‘boost::units::list&lt;boost::units::scale_list_dim&lt;boost::units::scale&lt;3600, boost::units::static_rational&lt;1&gt; &gt; &gt;, boost::units::dimensionless_type&gt;::item’ {aka ‘boost::units::scale_list_dim&lt;boost::units::scale&lt;3600, boost::units::static_rational&lt;1&gt; &gt; &gt;’}
  327 |             str += Begin::item::symbol();
      |                    ~~~~~~~~~~~~~~~~~~~^~
[...]
/usr/include/boost/units/io.hpp:393:37: error: ‘name’ is not a member of ‘boost::units::list&lt;boost::units::scale_list_dim&lt;boost::units::scale&lt;3600, boost::units::static_rational&lt;1&gt; &gt; &gt;, boost::units::dimensionless_type&gt;::item’ {aka ‘boost::units::scale_list_dim&lt;boost::units::scale&lt;3600, boost::units::static_rational&lt;1&gt; &gt; &gt;’}
  393 |             str += Begin::item::name();
      |                    ~~~~~~~~~~~~~~~~~^~

I tried defining the name_string and symbol_string which makes the second error disappear but not the first.

namespace boost
{
namespace units
{
std::string name_string(const watt_hour_unit&amp;)
{
    return &quot;Watt hours&quot;;
}

std::string symbol_string(const watt_hour_unit&amp;)
{
    return &quot;Wh&quot;;
}
}
}

答案1

得分: 1

只有在比例是10的某个幂或2的某个幂时,才能打印缩放单位。这是因为boost::units::scale专门针对这些特定的比例进行了优化。

您需要专门化boost::units::scale<3600, boost::units::static_rational<1>>

namespace bu = boost::units;
template <>
struct bu::scale<3600, bu::static_rational<1>>
{
      BOOST_STATIC_CONSTEXPR long base = 3600;
      typedef bu::static_rational<1> exponent;
      typedef double value_type;
      static BOOST_CONSTEXPR value_type value()   { return(3600); }
      static std::string name()   { return("x3600"); }
      static std::string symbol() { return("x3600"); }
};

"x3600"是我选择的任意字符串,用于表示3600的因子。仅当您不定义name_stringsymbol_string时,它将用于瓦时量的输出。因此,您需要这两个函数和这个专门化。

英文:

Scaled units only can be printed when the scale is a certain power of 10 or a certain power of 2. This is because boost::units::scale is specialized for those particular scales.

You need to specialize boost::units::scale&lt;3600, boost::units::static_rational&lt;1&gt;&gt;&gt;.

namespace bu = boost::units;
template &lt;&gt;
struct bu::scale&lt;3600, bu::static_rational&lt;1&gt;&gt;
{
      BOOST_STATIC_CONSTEXPR long base = 3600;
      typedef bu::static_rational&lt;1&gt; exponent;
      typedef double value_type;
      static BOOST_CONSTEXPR value_type value()   { return(3600); }
      static std::string name()   { return(&quot;x3600&quot;); }
      static std::string symbol() { return(&quot;x3600&quot;); }
};

"x3600" is an arbitrary string I chose to represent a factor of 3600. It will be used for output of watt-hour quantities only if you do not define name_string and symbol_string as you did. So you need both these functions and this specialization.

huangapple
  • 本文由 发表于 2023年7月31日 20:16:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定