|
Boost Users : |
Subject: [Boost-users] [units] (controlled) implicit conversion
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-04-29 05:20:32
Hi,
I took much labour in defining an extra system of units (atomic
units). It works very well and it also defines (optionaly) default
conversion from atomic to SI units. The problem that I have now is
that all the conversion has to be done explicitly. I understand that
this way of doing thing is by design.
But for some formulas I would like to allow implicit conversion, for
example when adding energies in SI and energies in atomic units (I
don't care which direction the conversion is done since I will
transform to one of the two at the end of the formula). On top of that
the Units Manual says:
"Safety and the potential for unintended conversions leading to
precision loss and hidden performance costs. Options are provided for
forcing implicit conversions between specific units to be allowed. "
The question is which are the "Options provided for forcing implicit
conversion"?
What follow is an example:
What I want is some controlled implicit conversion (even better is
this conversion are allowed inside a certain program scope). Note that
line 4 doesn't work:
quantity<si::energy> e1(3.*si::joules);
quantity<atomic::energy> e2(4.*atomic::hartree);
quantity<atomic::energy> e3(e1); //works
e1 = e2; //doesn't work
e1 = quantity<si::energy>(e2); //works
cout<<e1<<'\n'<<e2<<'\n'<<e3<<endl;
note that the third line works because somewhere I defined a
conversion_factor plus a default_conversion between units in the
different systems. And it works because the conversion is explicit.
Now the fourth line doesn't work, it gives an error (see at end).
comenting the third line compiles.
I even tried defining a conversion for this particular units, but
still doesn't work.
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(
boost::units::atomic::energy,
boost::units::si::energy,
double, 4.359744e-18
);
BOOST_UNITS_DEFAULT_CONVERSION(
boost::units::atomic::energy,
boost::units::si::energy
);
error caused by line 4:
In file included from /usr/include/boost/units/io.hpp:28,
from /home/alfc/prj/mag/Qbox//units/systems/
atomic.hpp:187,
from eos.cpp:8:
/usr/include/boost/units/quantity.hpp: In member function
boost::units::quantity<Unit, Y>& boost::units::quantity<Unit,
Y>::operator=(const boost::units::quantity<Unit2, YY>&) [with Unit2 =
boost::units::unit<boost::units::list<boost::units::dim<boost::units::length_base_dimension,
boost::units::static_rational<2l, 1l> >,
boost::units::list<boost::units::dim<boost::units::mass_base_dimension,
boost::units::static_rational<1l, 1l> >,
boost::units::list<boost::units::dim<boost::units::time_base_dimension,
boost::units::static_rational<-0x00000000000000002l, 1l> >,
boost::units::dimensionless_type> > >,
boost::units::homogeneous_system<boost::units::list<boost::units::atomic::electron_mass_base_unit,
boost::units::list<boost::units::atomic::elementary_charge_base_unit,
boost::units::list<boost::units::atomic::reduced_planck_constant_base_unit,
boost::units::list<boost::units::atomic::coulomb_force_constant_base_unit,
boost::units::list<boost::units::atomic::boltzman_constant_base_unit,
boost::units::dimensionless_type> > > > > >, void>, YY = double, Unit
=
boost::units::unit<boost::units::list<boost::units::dim<boost::units::length_base_dimension,
boost::units::static_rational<2l, 1l> >,
boost::units::list<boost::units::dim<boost::units::mass_base_dimension,
boost::units::static_rational<1l, 1l> >,
boost::units::list<boost::units::dim<boost::units::time_base_dimension,
boost::units::static_rational<-0x00000000000000002l, 1l> >,
boost::units::dimensionless_type> > >,
boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit,
boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit,
boost::units::scale<10l, boost::units::static_rational<3l, 1l> > >,
boost::units::list<boost::units::si::second_base_unit,
boost::units::list<boost::units::si::ampere_base_unit,
boost::units::list<boost::units::si::kelvin_base_unit,
boost::units::list<boost::units::si::mole_base_unit,
boost::units::list<boost::units::si::candela_base_unit,
boost::units::list<boost::units::angle::radian_base_unit,
boost::units::list<boost::units::angle::steradian_base_unit,
boost::units::dimensionless_type> > > > > > > > > >, void>, Y =
double]:
eos.cpp:115: instantiated from here
/usr/include/boost/units/quantity.hpp:234: error: invalid application
of sizeof to incomplete type
boost::STATIC_ASSERTION_FAILURE<false>
/usr/include/boost/units/quantity.hpp:234: error: invalid application
of sizeof to incomplete type
boost::STATIC_ASSERTION_FAILURE<false>
Thank you,
Alfredo
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net