On Tue, Aug 30, 2011 at 2:04 AM, Thomas Taylor
<thomas.taylor@univie.ac.at> wrote:
Could you please review the following patch for inclusion in boost::units?
*WHY*: Obviously this only defines a conversion for boost::units::one to a
numerical 1. When trying to convert at runtime (aiming for a runtime/dynamic
unit instead of the compiletime/static unit provided) I use the following
function:
template <typename U1, typename U2>
double unit_dispatch2t(U1 const& u1, U2 const& u2) {
return bu::conversion_factor(u1,u2);
}
where U1 and U2 are boost::unit::unit derivatives.
Without a conversion operator the compiler chickens out by claiming to not
be able to convert boost::units::one to (in my case) double. The added
conversion operator allows successfull compilation, thus allowing actual
runtime units (my application needs to handle calculations where some units
are known at compiletime and some only at runtime (e.g. the user wants the
calculation to be done in meters or millimeters)).
Oh, the runtime question. Here's how we've decided to approach this.
I'll just tell you, I find the compile-time safety attractive. For runtime, we've decided that calculations should be done in a base unit.
For anything else, we *convert to* a view unit, for display or report or what not.
Use case: calculations on volume, pressure, etc, done in base SI units. View may be in SI, US (English), or what have you.
HTH,
Thomas
--- one.hpp 2011-08-30 09:50:13.000000000 +0200
+++ one2.hpp 2011-08-30 09:50:07.000000000 +0200
@@ -19,3 +19,3 @@
-struct one { one() {} };
+struct one { one() {} operator int() { return 1; } };
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users