Opened 9 years ago
Last modified 9 years ago
#9582 new Feature Requests
Boost.Units: conversion to double with prefixed unit
| Reported by: | Owned by: | Matthias Schabel | |
|---|---|---|---|
| Milestone: | To Be Determined | Component: | units | 
| Version: | Boost 1.54.0 | Severity: | Cosmetic | 
| Keywords: | Units conversion prefix | Cc: | 
Description
A post to the Boost-users maillinglist suggested me to request this feature.
In the code below, I convert a double that is known to be in millimeters to a unit and back again. The latter fails, where it works as expected for meters.
I think that adding this feature increases the symmetry in the use of Boost.Units, as I have emphasised in my code.
I am aware of the great care the Boost.Units developers have in maintaining type-safety, so it might be that I am unaware of code compiling that shouldn't would this request be acknowledged.
using boost::units::conversion_factor;
using boost::units::quantity;
using boost::units::si::length;
using boost::units::si::meter;
using boost::units::si::milli;
typedef quantity<length> Length;
//This value is read from file
const double x_in_mm = 1.0;
//Add units
const Length x(x_in_mm * (milli * meter)); //Brackets added for emphasis
const double x_again_in_m  = x / meter; //Works
const double x_again_in_mm {
  x / (milli * meter) //Same brackets as at emphasis
}; //Why doesn't this?
assert(x_in_mm == x_again_in_mm);
      Change History (2)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
It can be done in C++11, I believe.
I checked with GCC 4.8.1 in C++1y mode: the unexpected behavior remains

The first case is an /explicit/ conversion. The second would require an implicit conversion since there's no way to define an explicit conversion to double in C++03. It can be done in C++11, I believe.