英文:
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<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";
}
}
}
答案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_string
和symbol_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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论