boostorg / type_traits

Boost.org type_traits module
http://boost.org/libs/type_traits
62 stars 82 forks source link

add is_integer #186

Closed gpeterhoff closed 9 months ago

gpeterhoff commented 9 months ago

is_integer.hpp

//  Distributed under the Boost Software License Version 1.0 https://www.boost.org/LICENSE_1_0.txt
//  Copyright Gero Peterhoff

#ifndef BOOST_TYPE_TRAITS_IS_INTEGER_HPP
#define BOOST_TYPE_TRAITS_IS_INTEGER_HPP

#include <boost/type_traits/remove_cvref.hpp>
#include <boost/type_traits/is_unsigned.hpp>
#include <boost/type_traits/is_signed.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/cstdint.hpp>

namespace boost
{
template <typename Type> struct is_integer          : public false_type{};
template <typename Type> struct is_integer<volatile const Type> : public is_integer<Type>{};
template <typename Type> struct is_integer<volatile Type>   : public is_integer<Type>{};
template <typename Type> struct is_integer<const Type>      : public is_integer<Type>{};

#if defined(BOOST_HAS_INT128)
template <> struct is_integer<boost::uint128_type>  : public true_type{};
template <> struct is_integer<boost::int128_type>   : public true_type{};
#endif

template <> struct is_integer<uint64_t> : public true_type{};
template <> struct is_integer<int64_t>  : public true_type{};

template <> struct is_integer<uint32_t> : public true_type{};
template <> struct is_integer<int32_t>  : public true_type{};

template <> struct is_integer<uint16_t> : public true_type{};
template <> struct is_integer<int16_t>  : public true_type{};

template <> struct is_integer<uint8_t>  : public true_type{};
template <> struct is_integer<int8_t>   : public true_type{};

template <typename Type> struct is_unsigned_integer : public integral_constant
<
    bool,
    is_integer<Type>::value && is_unsigned<Type>::value
>   {};

template <typename Type> struct is_signed_integer : public integral_constant
<
    bool,
    is_integer<Type>::value && is_signed<Type>::value
>   {};

#if !defined(BOOST_NO_CXX17_INLINE_VARIABLES)
template <typename Type> inline constexpr bool is_unsigned_integer_v    = is_unsigned_integer<Type>::value;
template <typename Type> inline constexpr bool is_signed_integer_v  = is_signed_integer<Type>::value;
template <typename Type> inline constexpr bool is_integer_v     = is_integer<Type>::value;
#endif
}   //  boost

#endif  //  BOOST_TYPE_TRAITS_IS_INTEGER_HPP
jzmaddock commented 9 months ago

This is effectively https://www.boost.org/doc/libs/1_84_0/libs/type_traits/doc/html/boost_typetraits/reference/is_integral.html, yes I realise you have a slightly different definition of what constitutes an integer, but was never our intention to provide super-fine grained categorisation.

gpeterhoff commented 9 months ago

That's exactly my point. You haven't thought about it, it doesn't exist in the standard. There are various use cases for this.

please open this issues

pdimov commented 9 months ago

The concept "integer type" does exist in the standard (but not with the above definition which is incorrect). See https://eel.is/c++draft/basic.fundamental#1 and https://eel.is/c++draft/basic.fundamental#2.

Then https://eel.is/c++draft/basic.fundamental#11 muddies the water somewhat, but I think that "signed integer type" + "unsigned integer type" is a better definition for "integer type".

Currently the best approximation of the above is `std::numeric_limits::is_integer", e.g. https://github.com/boostorg/core/blob/0a35bb6a20bd13e85ff492a5be01b3894807a0d1/include/boost/core/bit.hpp#L299.