Boost.Units Example: Convert between nautical mile and imperial foot

Hi, I am trying to use Boost.Units to convert between different units. I have implemented the example code for the "Radar Beam Height" [1] section but trying to convert between nautical mile and imperial foot result in a compiler error: I have: typedef boost::units::quantity<boost::units::si::length> Meters; typedef boost::units::quantity<imperial::length> Feet; typedef boost::units::quantity<nautical::length> NMiles; double func() { // Working const Meters si1(300.0*nautical::miles); const Meters si2(300.0*imperial::feet); const Feet feet1(300.0*imperial::feet); const Feet feet2(300.0*boost::units::si::meters); const NMiles mile1(300.0*nautical::miles); const NMiles mile2(300.0*boost::units::si::meters); // BROKEN const Feet feet3(300.0*nautical::miles); const NMiles mile3(300.0*imperial::feet); } The first few works, converting between meters and the new units, but converting between the two does not work. To see the issue see the code on Compiler Explorer here: https://gcc.godbolt.org/z/MpvuMy How can I get this working? [1] https://www.boost.org/doc/libs/1_68_0/doc/html/boost_units/Examples.html#boo... Regards,

On 27/09/2018 00:57, Carel Combrink wrote:
You've defined a conversion between imperial feet and meters, and a conversion between nautical miles and meters. But you haven't defined a direct conversion between imperial feet and nautical miles. It's apparently not smart enough to do the conversion through an unspecified intermediate unit. (This might be intentional, since it could be ambiguous or lossy.) You can either write your code to first convert the value into meters explicitly before converting it to the other unit: const Meters si1(300.0*nautical::miles); const NMiles mile4(si1); Or you can explicitly define the conversion you want (using the previously defined conversions as a base to avoid duplicating the factors): BOOST_UNITS_DEFINE_CONVERSION_FACTOR( imperial::length_base_unit, nautical::length_base_unit, double, boost::units::conversion_factor(imperial::foot, si::meter) * boost::units::conversion_factor(si::meter, nautical::mile));

On 27/09/2018 13:56, Steven Watanabe wrote:
The library has no way to know what the correct intermediate unit is.
Yes, that's what I said.
You can use BOOST_UNITS_DEFAULT_CONVERSION to specify. (Yes, I know this is kind of clunky).
Interesting. I was not aware of that (and that's not how I initially read the [minimal] docs for it, although now that you've said that I can see how it can be interpreted that way). Oddly, given these: BOOST_UNITS_DEFAULT_CONVERSION(imperial::length_base_unit, si::length); BOOST_UNITS_DEFAULT_CONVERSION(nautical::length_base_unit, si::length); Only one of these two lines actually needs to be defined in order to make both direct conversions (ft -> nmi and nmi -> ft) compile. Given that all three base units are independent, how does this work?

I expected as much. What lead me to believe that this was possible was the wording in the docs: "These units include conversions between themselves and the meter.", I was not sure what the "themselves" referred to, my assumption/hope was clearly wrong.
Can this *please *be added to the docs on that specific example. Thanks Steven and Gavin for the help, exactly what I was looking for. Regards,
participants (3)
-
Carel Combrink
-
Gavin Lambert
-
Steven Watanabe