Opened 8 years ago

Last modified 8 years ago

#9944 new Bugs

Regression test relies on undefined compiler behavior

Reported by: chris.cooper@… Owned by: No-Maintainer
Milestone: To Be Determined Component: utility
Version: Boost 1.54.0 Severity: Problem
Keywords: Cc:

Description

libs/utility/numeric_traits_test.cpp generates values complement_traits<Number>::min using a clever recursive template, but that template relies on left-shifting a negative value, and according to the C++11 standard that’s a no-no (“the behavior is undefined”) which means it’s not a constant expression, which means it can’t be calculated at compile time, which means the BOOST_STATIC_ASSERT in line 332 won’t compile, saying “static_assert expression is not an integral constant expression” (I’m using clang++).

The only use for the clever templates, as the comment in the file says, is for "platforms without <limits> support" so my proposed fix is:

#ifndef BOOST_NO_LIMITS template <class Number> struct complement_traits {

BOOST_STATIC_CONSTANT(Number, min = std::numeric_limits<Number>::min()); BOOST_STATIC_CONSTANT(Number, max = std::numeric_limits<Number>::max());

}; #else [SNIP] All of the other template definitions for complement_traits, complement_traits_aux, etc. #endif

Change History (3)

comment:1 by chris.cooper@…, 8 years ago

std::numeric_limits<Number>::min() and std::numeric_limits<Number>::max() are not constant expressions unless c++11 is specified, so a better fix is

#if !defined(BOOST_NO_LIMITS) && !defined(BOOST_NO_CXX11_CONSTEXPR)
template <class Number> struct complement_traits {
   BOOST_STATIC_CONSTANT(Number, min = std::numeric_limits<Number>::min());
   BOOST_STATIC_CONSTANT(Number, max = std::numeric_limits<Number>::max());
};
#else
[SNIP] All of the other template definitions for complement_traits, complement_traits_aux, etc.
#endif

comment:2 by chris.cooper@…, 8 years ago

Kim Barrett suggested using boost::integer_traits<Number>::const_min / const_max

It's not entirely clear what this file (numeric_traits_test.cpp) is testing - there is so much template stuff at the top, and then it looks like a lot of the tests are simply verifying that the templates work. There is data sent to std::cout which is perhaps helpful to developers, but not in an automated regression test. It appears the only thing that it tests (from an automated test point of view) is boost::detail::numeric_distance()?

comment:3 by Niklas Angare <li51ckf02@…>, 8 years ago

Component: Regression Testingutility
Owner: changed from René Rivera to No-Maintainer
Note: See TracTickets for help on using tickets.