[Units] conversion between double and millimeter
Hello Boost.Units users, When I convert a double to a unit (e.g. meter) and back, this works as expected. When I convert a double to a prefixed unit (e.g. millimeter) and back, this fails to compile. The code below shows how I'd expect it to work. What do I overlook? Thanks, Richel Bilderbeek const double x_in_mm = 1.0; const Length x(x_in_mm * milli * meter); //This works as expected: const double x_again_in_m = x / meter; //Why doesn't this: const double x_again_in_mm = x / (milli * meter); //Fails
On 14/01/2014 22:46, Quoth Richel Bilderbeek:
When I convert a double to a unit (e.g. meter) and back, this works as expected. When I convert a double to a prefixed unit (e.g. millimeter) and back, this fails to compile. The code below shows how I'd expect it to work. What do I overlook? Thanks, Richel Bilderbeek
const double x_in_mm = 1.0; const Length x(x_in_mm * milli * meter); //This works as expected: const double x_again_in_m = x / meter; //Why doesn't this: const double x_again_in_mm = x / (milli * meter); //Fails
I don't know why that doesn't work (it seems logical to me).
But (until someone chimes in with a better one) a somewhat ugly
workaround would be:
const double x_again_in_mm = x / meter * conversion_factor(meter, milli
* meter);
If you're doing this a lot, you could define a to_mm(x) helper that does
the above, or you could define a new system with a millimeters base unit:
typedef scaled_base_unit
millimeter_base_unit; // I don't know if there's a way to extract the scale from "milli"
typedef make_system
participants (2)
-
Gavin Lambert
-
Richel Bilderbeek