gcc/libstdc++-v3/include/std/utility
Jonathan Wakely c03b53da91 libstdc++: Add lightweight replacement for std::numeric_limits (PR 92546)
Many uses of std::numeric_limits in C++17 and C++20 features only really
need the min(), max() and digits constants for integral types. By adding
__detail::__int_limits we can avoid including the whole <limits> header.

The <limits> header isn't especially large, but avoiding it still gives
small savings in compilation time and memory usage for the compiler.

There are also C++11 features that could benefit from this change (e.g.
<bits/hashtable_policy.h> and <bits/uniform_int_dist.h>) but I won't
change those until stage 1.

The implementation of __int_limits assumes two's complement integers,
which is true for all targets supported by GCC.

	PR libstdc++/92546 (partial)
	* include/Makefile.am: Add new header.
	* include/Makefile.in: Regenerate.
	* include/bits/int_limits.h: New header.
	* include/bits/parse_numbers.h (__select_int::_Select_int): Replace
	numeric_limits with __detail::__int_limits.
	* include/std/bit (__rotl, __rotr, __countl_zero, __countl_one)
	(__countr_zero, __countr_one, __popcount, __ceil2, __floor2, __log2p1):
	Likewise.
	* include/std/charconv (__to_chars_8, __from_chars_binary)
	(__from_chars_alpha_to_num, from_chars): Likewise.
	* include/std/memory_resource (polymorphic_allocator::allocate)
	(polymorphic_allocator::allocate_object): Likewise.
	* include/std/string_view (basic_string_view::_S_compare): Likewise.
	* include/std/utility (in_range): Likewise.
	* testsuite/20_util/integer_comparisons/in_range_neg.cc: Adjust for
	extra error about incomplete type __int_limits<bool>.
	* testsuite/26_numerics/bit/bit.count/countl_one.cc: Include <limits>.
	* testsuite/26_numerics/bit/bit.count/countl_zero.cc: Likewise.
	* testsuite/26_numerics/bit/bit.count/countr_one.cc: Likewise.
	* testsuite/26_numerics/bit/bit.count/countr_zero.cc: Likewise.
	* testsuite/26_numerics/bit/bit.count/popcount.cc: Likewise.
	* testsuite/26_numerics/bit/bit.pow.two/ceil2_neg.cc: Likewise.
	* testsuite/26_numerics/bit/bit.pow.two/ceil2.cc: Likewise.
	* testsuite/26_numerics/bit/bit.pow.two/floor2.cc: Likewise.
	* testsuite/26_numerics/bit/bit.pow.two/ispow2.cc: Likewise.
	* testsuite/26_numerics/bit/bit.pow.two/log2p1.cc: Likewise.
	* testsuite/26_numerics/bit/bit.rotate/rotl.cc: Likewise.
	* testsuite/26_numerics/bit/bit.rotate/rotr.cc: Likewise.
2020-02-17 15:11:04 +00:00

483 lines
14 KiB
C++

// <utility> -*- C++ -*-
// Copyright (C) 2001-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1996,1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/** @file include/utility
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_UTILITY
#define _GLIBCXX_UTILITY 1
#pragma GCC system_header
/**
* @defgroup utilities Utilities
*
* Components deemed generally useful. Includes pair, tuple,
* forward/move helpers, ratio, function object, metaprogramming and
* type traits, time, date, and memory functions.
*/
#include <bits/c++config.h>
#include <bits/stl_relops.h>
#include <bits/stl_pair.h>
#if __cplusplus >= 201103L
#include <type_traits>
#include <bits/move.h>
#include <initializer_list>
#if __cplusplus > 201703L
#include <bits/int_limits.h>
#endif
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Finds the size of a given tuple type.
template<typename _Tp>
struct tuple_size;
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2313. tuple_size should always derive from integral_constant<size_t, N>
// 2770. tuple_size<const T> specialization is not SFINAE compatible
template<typename _Tp,
typename _Up = typename remove_cv<_Tp>::type,
typename = typename enable_if<is_same<_Tp, _Up>::value>::type,
size_t = tuple_size<_Tp>::value>
using __enable_if_has_tuple_size = _Tp;
template<typename _Tp>
struct tuple_size<const __enable_if_has_tuple_size<_Tp>>
: public tuple_size<_Tp> { };
template<typename _Tp>
struct tuple_size<volatile __enable_if_has_tuple_size<_Tp>>
: public tuple_size<_Tp> { };
template<typename _Tp>
struct tuple_size<const volatile __enable_if_has_tuple_size<_Tp>>
: public tuple_size<_Tp> { };
/// Gives the type of the ith element of a given tuple type.
template<std::size_t __i, typename _Tp>
struct tuple_element;
// Duplicate of C++14's tuple_element_t for internal use in C++11 mode
template<std::size_t __i, typename _Tp>
using __tuple_element_t = typename tuple_element<__i, _Tp>::type;
template<std::size_t __i, typename _Tp>
struct tuple_element<__i, const _Tp>
{
typedef typename add_const<__tuple_element_t<__i, _Tp>>::type type;
};
template<std::size_t __i, typename _Tp>
struct tuple_element<__i, volatile _Tp>
{
typedef typename add_volatile<__tuple_element_t<__i, _Tp>>::type type;
};
template<std::size_t __i, typename _Tp>
struct tuple_element<__i, const volatile _Tp>
{
typedef typename add_cv<__tuple_element_t<__i, _Tp>>::type type;
};
#if __cplusplus >= 201402L
// The standard says this macro and alias template should be in <tuple>
// but we define them here, to be available when the partial specializations
// of tuple_element<pair<T,U>> and tuple_element<array<T,N>> are defined.
#define __cpp_lib_tuple_element_t 201402L
template<std::size_t __i, typename _Tp>
using tuple_element_t = typename tuple_element<__i, _Tp>::type;
#endif
// Various functions which give std::pair a tuple-like interface.
/// Partial specialization for std::pair
template<typename _T1, typename _T2>
struct __is_tuple_like_impl<std::pair<_T1, _T2>> : true_type
{ };
/// Partial specialization for std::pair
template<class _Tp1, class _Tp2>
struct tuple_size<std::pair<_Tp1, _Tp2>>
: public integral_constant<std::size_t, 2> { };
/// Partial specialization for std::pair
template<class _Tp1, class _Tp2>
struct tuple_element<0, std::pair<_Tp1, _Tp2>>
{ typedef _Tp1 type; };
/// Partial specialization for std::pair
template<class _Tp1, class _Tp2>
struct tuple_element<1, std::pair<_Tp1, _Tp2>>
{ typedef _Tp2 type; };
template<std::size_t _Int>
struct __pair_get;
template<>
struct __pair_get<0>
{
template<typename _Tp1, typename _Tp2>
static constexpr _Tp1&
__get(std::pair<_Tp1, _Tp2>& __pair) noexcept
{ return __pair.first; }
template<typename _Tp1, typename _Tp2>
static constexpr _Tp1&&
__move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
{ return std::forward<_Tp1>(__pair.first); }
template<typename _Tp1, typename _Tp2>
static constexpr const _Tp1&
__const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
{ return __pair.first; }
template<typename _Tp1, typename _Tp2>
static constexpr const _Tp1&&
__const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
{ return std::forward<const _Tp1>(__pair.first); }
};
template<>
struct __pair_get<1>
{
template<typename _Tp1, typename _Tp2>
static constexpr _Tp2&
__get(std::pair<_Tp1, _Tp2>& __pair) noexcept
{ return __pair.second; }
template<typename _Tp1, typename _Tp2>
static constexpr _Tp2&&
__move_get(std::pair<_Tp1, _Tp2>&& __pair) noexcept
{ return std::forward<_Tp2>(__pair.second); }
template<typename _Tp1, typename _Tp2>
static constexpr const _Tp2&
__const_get(const std::pair<_Tp1, _Tp2>& __pair) noexcept
{ return __pair.second; }
template<typename _Tp1, typename _Tp2>
static constexpr const _Tp2&&
__const_move_get(const std::pair<_Tp1, _Tp2>&& __pair) noexcept
{ return std::forward<const _Tp2>(__pair.second); }
};
template<std::size_t _Int, class _Tp1, class _Tp2>
constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
get(std::pair<_Tp1, _Tp2>& __in) noexcept
{ return __pair_get<_Int>::__get(__in); }
template<std::size_t _Int, class _Tp1, class _Tp2>
constexpr typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
get(std::pair<_Tp1, _Tp2>&& __in) noexcept
{ return __pair_get<_Int>::__move_get(std::move(__in)); }
template<std::size_t _Int, class _Tp1, class _Tp2>
constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&
get(const std::pair<_Tp1, _Tp2>& __in) noexcept
{ return __pair_get<_Int>::__const_get(__in); }
template<std::size_t _Int, class _Tp1, class _Tp2>
constexpr const typename tuple_element<_Int, std::pair<_Tp1, _Tp2>>::type&&
get(const std::pair<_Tp1, _Tp2>&& __in) noexcept
{ return __pair_get<_Int>::__const_move_get(std::move(__in)); }
#if __cplusplus > 201103L
#define __cpp_lib_tuples_by_type 201304
template <typename _Tp, typename _Up>
constexpr _Tp&
get(pair<_Tp, _Up>& __p) noexcept
{ return __p.first; }
template <typename _Tp, typename _Up>
constexpr const _Tp&
get(const pair<_Tp, _Up>& __p) noexcept
{ return __p.first; }
template <typename _Tp, typename _Up>
constexpr _Tp&&
get(pair<_Tp, _Up>&& __p) noexcept
{ return std::move(__p.first); }
template <typename _Tp, typename _Up>
constexpr const _Tp&&
get(const pair<_Tp, _Up>&& __p) noexcept
{ return std::move(__p.first); }
template <typename _Tp, typename _Up>
constexpr _Tp&
get(pair<_Up, _Tp>& __p) noexcept
{ return __p.second; }
template <typename _Tp, typename _Up>
constexpr const _Tp&
get(const pair<_Up, _Tp>& __p) noexcept
{ return __p.second; }
template <typename _Tp, typename _Up>
constexpr _Tp&&
get(pair<_Up, _Tp>&& __p) noexcept
{ return std::move(__p.second); }
template <typename _Tp, typename _Up>
constexpr const _Tp&&
get(const pair<_Up, _Tp>&& __p) noexcept
{ return std::move(__p.second); }
#define __cpp_lib_exchange_function 201304
#if __cplusplus > 201703L
# define __cpp_lib_constexpr_algorithms 201806L
#endif
/// Assign @p __new_val to @p __obj and return its previous value.
template <typename _Tp, typename _Up = _Tp>
_GLIBCXX20_CONSTEXPR
inline _Tp
exchange(_Tp& __obj, _Up&& __new_val)
{ return std::__exchange(__obj, std::forward<_Up>(__new_val)); }
#endif
// Stores a tuple of indices. Used by tuple and pair, and by bind() to
// extract the elements in a tuple.
template<size_t... _Indexes> struct _Index_tuple { };
#ifdef __has_builtin
# if __has_builtin(__make_integer_seq)
# define _GLIBCXX_USE_MAKE_INTEGER_SEQ 1
# endif
#endif
// Builds an _Index_tuple<0, 1, 2, ..., _Num-1>.
template<size_t _Num>
struct _Build_index_tuple
{
#if _GLIBCXX_USE_MAKE_INTEGER_SEQ
template<typename, size_t... _Indices>
using _IdxTuple = _Index_tuple<_Indices...>;
using __type = __make_integer_seq<_IdxTuple, size_t, _Num>;
#else
using __type = _Index_tuple<__integer_pack(_Num)...>;
#endif
};
#if __cplusplus > 201103L
#define __cpp_lib_integer_sequence 201304
/// Class template integer_sequence
template<typename _Tp, _Tp... _Idx>
struct integer_sequence
{
typedef _Tp value_type;
static constexpr size_t size() noexcept { return sizeof...(_Idx); }
};
/// Alias template make_integer_sequence
template<typename _Tp, _Tp _Num>
using make_integer_sequence
#if _GLIBCXX_USE_MAKE_INTEGER_SEQ
= __make_integer_seq<integer_sequence, _Tp, _Num>;
#else
= integer_sequence<_Tp, __integer_pack(_Num)...>;
#endif
#undef _GLIBCXX_USE_MAKE_INTEGER_SEQ
/// Alias template index_sequence
template<size_t... _Idx>
using index_sequence = integer_sequence<size_t, _Idx...>;
/// Alias template make_index_sequence
template<size_t _Num>
using make_index_sequence = make_integer_sequence<size_t, _Num>;
/// Alias template index_sequence_for
template<typename... _Types>
using index_sequence_for = make_index_sequence<sizeof...(_Types)>;
#endif
#if __cplusplus > 201402L
struct in_place_t {
explicit in_place_t() = default;
};
inline constexpr in_place_t in_place{};
template<typename _Tp> struct in_place_type_t
{
explicit in_place_type_t() = default;
};
template<typename _Tp>
inline constexpr in_place_type_t<_Tp> in_place_type{};
template<size_t _Idx> struct in_place_index_t
{
explicit in_place_index_t() = default;
};
template<size_t _Idx>
inline constexpr in_place_index_t<_Idx> in_place_index{};
template<typename>
struct __is_in_place_type_impl : false_type
{ };
template<typename _Tp>
struct __is_in_place_type_impl<in_place_type_t<_Tp>> : true_type
{ };
template<typename _Tp>
struct __is_in_place_type
: public __is_in_place_type_impl<_Tp>
{ };
#define __cpp_lib_as_const 201510
template<typename _Tp>
constexpr add_const_t<_Tp>& as_const(_Tp& __t) noexcept { return __t; }
template<typename _Tp>
void as_const(const _Tp&&) = delete;
#if __cplusplus > 201703L
#define __cpp_lib_integer_comparison_functions 202002L
template<typename _Tp, typename _Up>
constexpr bool
cmp_equal(_Tp __t, _Up __u) noexcept
{
static_assert(__is_standard_integer<_Tp>::value);
static_assert(__is_standard_integer<_Up>::value);
if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
return __t == __u;
else if constexpr (is_signed_v<_Tp>)
return __t >= 0 && make_unsigned_t<_Tp>(__t) == __u;
else
return __u >= 0 && __t == make_unsigned_t<_Up>(__u);
}
template<typename _Tp, typename _Up>
constexpr bool
cmp_not_equal(_Tp __t, _Up __u) noexcept
{ return !std::cmp_equal(__t, __u); }
template<typename _Tp, typename _Up>
constexpr bool
cmp_less(_Tp __t, _Up __u) noexcept
{
static_assert(__is_standard_integer<_Tp>::value);
static_assert(__is_standard_integer<_Up>::value);
if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
return __t < __u;
else if constexpr (is_signed_v<_Tp>)
return __t < 0 || make_unsigned_t<_Tp>(__t) < __u;
else
return __u >= 0 && __t < make_unsigned_t<_Up>(__u);
}
template<typename _Tp, typename _Up>
constexpr bool
cmp_greater(_Tp __t, _Up __u) noexcept
{ return std::cmp_less(__u, __t); }
template<typename _Tp, typename _Up>
constexpr bool
cmp_less_equal(_Tp __t, _Up __u) noexcept
{ return !std::cmp_less(__u, __t); }
template<typename _Tp, typename _Up>
constexpr bool
cmp_greater_equal(_Tp __t, _Up __u) noexcept
{ return !std::cmp_less(__t, __u); }
template<typename _Up, typename _Tp>
constexpr bool
in_range(_Tp __t) noexcept
{
static_assert(__is_standard_integer<_Up>::value);
static_assert(__is_standard_integer<_Tp>::value);
using std::__detail::__int_limits;
if constexpr (is_signed_v<_Tp> == is_signed_v<_Up>)
return __int_limits<_Up>::min() <= __t
&& __t <= __int_limits<_Up>::max();
else if constexpr (is_signed_v<_Tp>)
return __t >= 0
&& make_unsigned_t<_Tp>(__t) <= __int_limits<_Up>::max();
else
return __t <= make_unsigned_t<_Up>(__int_limits<_Up>::max());
}
#endif // C++20
#endif // C++17
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace
#endif
#endif /* _GLIBCXX_UTILITY */