std::numeri...::is_integer(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME

okay so bestie, welcome to std::numeric_limits::is_integer β€” like, the whole page is literally just std::numeric_limits::is_integer, and ngl the manual name at the top is std::numeri...::is_integer(3) because even the manpage got truncation energy. anyway. this is our little corner of the C++ Standard Library and it's lowkey iconic ✨


✨ SYNOPSIS

the way this is literally just one bool that changed its outfit in C++11:

static const bool is_integer;      (until C++11)
static constexpr bool is_integer;  (since C++11)

so, the tea: the value of std::numeric_limits<T>::is_integer is true for all integer arithmetic types T and false otherwise. and this constant is meaningful for all specializations β€” no "sorry not applicable" nonsense, it always has something to say πŸ’…


πŸŽ€ STANDARD SPECIALIZATIONS

here's the vibe check for every T, matching up with the value of std::numeric_limits<T>::is_integer:

  • /* non-specialized */ β€” false
  • bool β€” true
  • char β€” true
  • signed char β€” true
  • unsigned char β€” true
  • wchar_t β€” true
  • char8_t (since C++20) β€” true
  • char16_t (since C++11) β€” true
  • char32_t (since C++11) β€” true
  • short β€” true
  • unsigned short β€” true
  • int β€” true
  • unsigned int β€” true
  • long β€” true
  • unsigned long β€” true
  • long long (since C++11) β€” true
  • unsigned long long (since C++11) β€” true
  • float β€” false
  • double β€” false
  • long double β€” false

okay so the pattern is giving "if it's whole-number coded it's true, if it's floating-point coded it's false" πŸ¦‹ and bool is in the true club, which honestly? deserved πŸ‘‘


πŸ”₯ EXAMPLE

run this code, bestie, it's all static_assert so if it compiles you already won:

// Run this code

#include <cstddef>
#include <cstdint>
#include <limits>

static_assert
(
        std::numeric_limits<bool>::is_integer
    &&  std::numeric_limits<std::size_t>::is_integer
    &&  std::numeric_limits<std::int32_t>::is_integer
    &&  std::numeric_limits<std::int64_t>::is_integer
    &&  std::numeric_limits<decltype(42)>::is_integer
    && !std::numeric_limits<int*>::is_integer
    && !std::numeric_limits<float>::is_integer
    && !std::numeric_limits<double>::is_integer
    && !std::numeric_limits<long double>::is_integer
    && !std::numeric_limits<decltype([](){})>::is_integer // P0315R4
);

int main() {}

the way std::numeric_limits<decltype(42)>::is_integer is just int in a trench coat and still ate 😭 and yes, int* and the lambda (// P0315R4) are very much not integers, no cap.


🌈 SEE ALSO

the squad, all linked up:

  • is_integral β€” checks if a type is an integral type, (C++11) (class template)
  • is_signed β€” identifies signed types, [static] (public static member constant)
  • is_exact β€” identifies exact types, [static] (public static member constant)
  • is_bounded β€” identifies types that represent a finite set of values, [static] (public static member constant)

okay that's the whole page, you ate that fr fr 🫢 this iconic little slice of documentation came to you from cppreference.com, dated 2024.06.10, and it was called std::numeri...::is_integer(3) β€” mwah, periodt πŸ’‹πŸŒΈ

πŸ“¦ where this page comes from