Skip to content

<numeric>: Consider avoiding <limits> inclusion #832

Description

Currently, <numeric> includes <limits> in C++20 mode:

STL/stl/inc/numeric

Lines 13 to 15 in 46ae5f8

#if _HAS_CXX20
#include <limits>
#endif // _HAS_CXX20

<limits> is a relatively large header (potentially affecting compiler throughput, i.e. slowing down builds), but our usage is very limited here - we just need floating-point min/max:

STL/stl/inc/numeric

Lines 640 to 641 in 46ae5f8

if constexpr (is_floating_point_v<_Ty>) {
constexpr _Ty _High_limit = (numeric_limits<_Ty>::max)() / 2;

constexpr _Ty _Low_limit = (numeric_limits<_Ty>::min)() * 2;

<numeric> always includes <xutility> which includes <climits>:

#include <xutility>

#include <climits>

So <numeric> could directly use the classic macros:

STL/stl/inc/limits

Lines 872 to 878 in 46ae5f8

_NODISCARD static constexpr float(min)() noexcept {
return FLT_MIN;
}
_NODISCARD static constexpr float(max)() noexcept {
return FLT_MAX;
}

STL/stl/inc/limits

Lines 921 to 927 in 46ae5f8

_NODISCARD static constexpr double(min)() noexcept {
return DBL_MIN;
}
_NODISCARD static constexpr double(max)() noexcept {
return DBL_MAX;
}

STL/stl/inc/limits

Lines 970 to 976 in 46ae5f8

_NODISCARD static constexpr long double(min)() noexcept {
return LDBL_MIN;
}
_NODISCARD static constexpr long double(max)() noexcept {
return LDBL_MAX;
}

Specifically, the changes in <numeric> I'm envisioning are:

  1. Stop including <limits>.
  2. Define a helper variable template _Floating_min<_Ty>, specialized for float, double, and long double.
  3. Similarly define _Floating_max<_Ty>.
  4. Replace the numeric_limits calls with uses of the variable templates. Note that because users could theoretically pass volatile values, the uses should say remove_cv_t<_Ty>; numeric_limits was previously handling that:

STL/stl/inc/limits

Lines 108 to 118 in 46ae5f8

template <class _Ty>
class numeric_limits<const _Ty> : public numeric_limits<_Ty> { // numeric limits for const types
};
template <class _Ty>
class numeric_limits<volatile _Ty> : public numeric_limits<_Ty> { // numeric limits for volatile types
};
template <class _Ty>
class numeric_limits<const volatile _Ty> : public numeric_limits<_Ty> { // numeric limits for const volatile types
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixedSomething works now, yay!throughputMust compile faster

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions