Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost.Units Example: Convert between nautical mile and imperial foot
From: Gavin Lambert (boost_at_[hidden])
Date: 2018-09-27 01:30:56


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

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


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