[units] implicit type conversions to specific unit quantity

Hello, I'd like to convert from an SI length quantity to a US inches quantity. I believe the following snippet will do what I want it to, but wanted to clarify if that was the case: http://www.boost.org/doc/libs/1_59_0/doc/html/boost_units/Quantities.html I am referencing the online docs, but needed some clarity in actually how to go about using it in practice: <code> using boost::units::si::meter_base_unit; using boost::units::us::inch_base_unit; // i.e. 0.3048 * 12 (inches/feet, feet/meter) BOOST_UNITS_DEFINE_CONVERSION_FACTOR(inch_base_unit, meter_base_unit, double, 3.6576); //... quantity<inch_unit> to_inches_length_quantity(quantity<length> const & L) { const quantity<inch_unit::unit_type> result_(L); return result_; } </code> Just besides that, there isn't a cleaner way of doing it? Some have mentioned going as far as creating a whole other system for my calculator's purposes, for example. Thank you... Best regards, Michael Powell

On Sat, Aug 15, 2015 at 12:32 PM, Michael Powell <mwpowellhtx@gmail.com> wrote:
Hello,
I'd like to convert from an SI length quantity to a US inches quantity. I believe the following snippet will do what I want it to, but wanted to clarify if that was the case:
http://www.boost.org/doc/libs/1_59_0/doc/html/boost_units/Quantities.html
I am referencing the online docs, but needed some clarity in actually how to go about using it in practice:
<code> using boost::units::si::meter_base_unit; using boost::units::us::inch_base_unit;
// i.e. 0.3048 * 12 (inches/feet, feet/meter) BOOST_UNITS_DEFINE_CONVERSION_FACTOR(inch_base_unit, meter_base_unit, double, 3.6576);
//...
quantity<inch_unit> to_inches_length_quantity(quantity<length> const & L) { const quantity<inch_unit::unit_type> result_(L); return result_; } </code>
I'm not sure I wasn't at a similar spot as with Ryan in a prior thread. https://groups.google.com/forum/#!topic/boost-list/3kx_vFpzHSc I was able to move the typedefs, etc, into a source file, and the external symbols resolved properly. Also, I'm not positive, the inches <-> meters length conversion isn't already defined? At least some errors leading up to this point seem to suggest that perhaps it is. Although I want to be careful that meters <-> inches is correct and that yards aren't getting in the way.
Just besides that, there isn't a cleaner way of doing it? Some have mentioned going as far as creating a whole other system for my calculator's purposes, for example.
Thank you...
Best regards,
Michael Powell

On 16/08/2015 05:18, Michael Powell wrote:
Also, I'm not positive, the inches <-> meters length conversion isn't already defined? At least some errors leading up to this point seem to suggest that perhaps it is. Although I want to be careful that meters <-> inches is correct and that yards aren't getting in the way.
Inches are defined as a scaled version of yards, and a conversion between yards and meters is defined, so it is redundant (and not possible) to also define a conversion between meters and inches. After omitting the conversion factor, you also need to correct your function: typedef inch_base_unit::unit_type inch_unit; quantity<inch_unit> to_inches_length_quantity(quantity<length> const & L) { const quantity<inch_unit> result_(L); return result_; }
participants (2)
-
Gavin Lambert
-
Michael Powell