Opened 10 years ago

Last modified 10 years ago

#6872 new Bugs

units/io.hpp: autoprefix_norm_impl<> should resolve to no-op for unsigned built-in types

Reported by: Vassilii Khachaturov <vkh@…> Owned by: Steven Watanabe
Milestone: To Be Determined Component: units
Version: Boost Development Trunk Severity: Problem
Keywords: Cc:

Description

The code

using namespace boost::units; const quantity<si::length, int> boa = 5 * si::meters; std::cout << boa << std::endl;

prints "5 m", as expected, yet

using namespace boost::units; const quantity<si::length, unsigned int> boa = 5 * si::meters; std::cout << boa << std::endl;

fails to compile due to the std::abs call being ambiguous in io.hpp: template<class T> struct autoprefix_norm_impl<T, true> {

typedef double type; static double call(const T& arg) { return std::abs(arg); }

};

There should be a specialization resolving to a no-op (just return the arg) for unsigned types.

A (probably slightly heavier) user-side workaround possible one can use is to forcibly define the missing std::abs to be a no-op as follows:

namespace std {

template<typename Unsigned> typename boost::enable_if<boost::is_unsigned<Unsigned>, Unsigned>::type

abs(const Unsigned& x)

{

return x; unsigned type...

}

}

Alternatively, boost::units should do a static assert that the type underlying the quantity is signed if that is what is intended (but why restrict it thus?)

Found and tested the workaround on 1.47.0, but confirmed to still exist on the trunk.

Change History (1)

comment:1 by Matthias Schabel, 10 years ago

Owner: changed from Matthias Schabel to Steven Watanabe
Note: See TracTickets for help on using tickets.