While it is not (yet) standards compliant, most current C++ compilers support this,
which is about as easy as you can get :
typedef typeof(si::giga*si::pascals) gigapascal_type;
ok, that is useful, I ended up doing this
namespace si{
template<class Unit> struct femto_scaled{typedef typename
make_scaled_unit > >::type
unit;};
template<class Unit> struct pico_scaled {typedef typename
make_scaled_unit > >::type
unit;};
template<class Unit> struct nano_scaled {typedef typename
make_scaled_unit > >::type
unit;};
template<class Unit> struct mega_scaled {typedef typename
make_scaled_unit > >::type
unit;};
template<class Unit> struct giga_scaled {typedef typename
make_scaled_unit > >::type
unit;};
//name "femto" instead of "femto_scaled" would clash with si::femto
constant
}
which works pretty well, for example
quantitysi::time::unit>
one_fs(1.*si::femto*si::second);
quantitysi::time::unit> one_s(1.*si::second);
quantityatomic::energy::unit>
one_nanohartree(1.*si::nano*atomic::hartree);
quantityatomic::energy::unit>
one_hartree(1.*atomic::hartree);
BUT for some strange reason doesn't completely work for this unit
(electronvolt) defined outside a system:
quantityatomic::electron_volt_unit::unit>
one_Mev(1.*si::mega*atomic::eV); //works
quantityatomic::electron_volt_unit::unit>
one_ev(1.*atomic::eV); //does not compile
electron_volt is defined as
namespace atomic{
struct electron_volt_base_unit : base_unit {
static const std::string name() { return "electronvolt";}
static const std::string symbol() { return "eV"; }
};
typedef electron_volt_base_unit::unit_type electron_volt_unit;
static const electron_volt_unit electron_volt, eV;
inline std::string symbol_string(const
reduce_unit::type&){return "eV";}
}
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(
boost::units::atomic::electron_volt_base_unit,
boost::units::atomic::energy,
double, 0.03674932
);
BOOST_UNITS_DEFAULT_CONVERSION(
boost::units::atomic::electron_volt_base_unit,
boost::units::atomic::energy
);
and the compiler error is
In file included from /usr/include/boost/units/
heterogeneous_system.hpp:36,
from /usr/include/boost/units/base_unit.hpp:15,
from .atomic.hpp.cpp.cpp:14:
/usr/include/boost/units/detail/linear_algebra.hpp: In instantiation
of
‘boost::units::detail::determine_extra_equations_skip_zeros_impl::apply, boost::units::list,
boost::units::dimensionless_type> >,
boost::units::dimensionless_type>, 1, 1, 3,
boost::units::list, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::list, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::dimensionless_type> > >’:
/usr/include/boost/units/detail/linear_algebra.hpp:264: instantiated
from ‘boost::units::detail::determine_extra_equations<2,
false>::apply, boost::units::list,
boost::units::dimensionless_type> >,
boost::units::dimensionless_type>, 3,
boost::units::list, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::list, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::dimensionless_type> > >’
/usr/include/boost/units/detail/linear_algebra.hpp:264: instantiated
from ‘boost::units::detail::determine_extra_equations<3,
false>::apply, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::list, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::dimensionless_type> >, 3,
boost::units::list, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::list, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::dimensionless_type> > >’
/usr/include/boost/units/detail/linear_algebra.hpp:538: instantiated
from
‘boost::units::detail::make_square_and_invert, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::list, boost::units::list,
boost::units::list, boost::units::dimensionless_type> > >,
boost::units::dimensionless_type> > >’
/usr/include/boost/units/detail/linear_algebra.hpp:828: instantiated
from
‘boost::units::detail::normalize_units > >’
/usr/include/boost/units/detail/linear_algebra.hpp:1035:
instantiated from
‘boost::units::detail::calculate_base_unit_exponents_impl<false>::apply >,
boost::units::list >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > > >’
/usr/include/boost/units/detail/linear_algebra.hpp:1051:
instantiated from
‘boost::units::detail::calculate_base_unit_exponents >,
boost::units::list >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > > >’
/usr/include/boost/units/heterogeneous_system.hpp:240: instantiated
from
‘boost::units::detail::make_heterogeneous_system >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::homogeneous_system > > >’
/usr/include/boost/units/unit.hpp:93: instantiated from
‘boost::units::reduce_unit >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::homogeneous_system > >, void> >’
/usr/include/boost/units/detail/conversion_impl.hpp:314:
instantiated from
‘boost::units::detail::conversion_impl<1>::apply >,
boost::units::dimensionless_type>,
boost::units::homogeneous_system > > >’
/usr/include/boost/units/detail/conversion_impl.hpp:445:
instantiated from
‘boost::units::detail::conversion_factor_helper >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::heterogeneous_system >,
boost::units::dimensionless_type>,
boost::units::list >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::dimensionless_type> >, void>,
boost::units::unit >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::heterogeneous_system >,
boost::units::dimensionless_type>,
boost::units::list >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::list > >,
boost::units::dimensionless_type> > >, void> >’
/usr/include/boost/units/detail/conversion_impl.hpp:345:
instantiated from ‘static boost::units::quantity
boost::units::conversion_helper,
boost::units::quantity >::convert(const
boost::units::quantity&) [with Unit1 =
boost::units::unit >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::heterogeneous_system >,
boost::units::dimensionless_type>,
boost::units::list >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::dimensionless_type> >, void>, T1 = double, Unit2 =
boost::units::unit >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::heterogeneous_system >,
boost::units::dimensionless_type>,
boost::units::list >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::list > >,
boost::units::dimensionless_type> > >, void>, T2 = double]’
/usr/include/boost/units/quantity.hpp:193: instantiated from
‘boost::units::quantity::quantity(const
boost::units::quantity&, typename
boost::disable_if::type,
boost::units::detail::is_non_narrowing_conversion,
mpl_::bool_<true>, mpl_::bool_<true>, mpl_::bool_<true> >, typename
boost::units::detail::disable_if_is_same::type>::type*)
[with Unit2 =
boost::units::unit >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::heterogeneous_system >,
boost::units::dimensionless_type>,
boost::units::list >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::dimensionless_type> >, void>, YY = double, Unit =
boost::units::unit >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::heterogeneous_system >,
boost::units::dimensionless_type>,
boost::units::list >,
boost::units::list >,
boost::units::list >,
boost::units::dimensionless_type> > >,
boost::units::list > >,
boost::units::dimensionless_type> > >, void>, Y = double]’
.atomic.hpp.cpp.cpp:416: instantiated from here
/usr/include/boost/units/detail/linear_algebra.hpp:197: error: no type
named ‘item’ in ‘struct boost::units::dimensionless_type’
compilation terminated due to -Wfatal-errors.