c++boostboost-units

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

Solution

  • 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<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" 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.