Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost.Units Example: Convert between nautical mile and imperial foot
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2018-09-27 01:56:40


AMDG

On 09/26/2018 07:30 PM, Gavin Lambert via Boost-users wrote:
> On 27/09/2018 00:57, Carel Combrink wrote:
>> 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:
> [...]
>> 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
>
> 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.)
>

The library has no way to know what the correct
intermediate unit is. You can use
BOOST_UNITS_DEFAULT_CONVERSION to specify.
(Yes, I know this is kind of clunky).

> 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));
>

In Christ,
Steven Watanabe


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