std::in_range(3)

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

πŸ’– std::in_range β€” the vibe check your integers deserve

okay so bestie, welcome to the std::in_range page from the C++ Standard Library manual (that's section 3, periodt). Buckle up, this one's short and it slays πŸŽ€


✨ NAME ✨

std::in_range β€” literally just std::in_range. That's the name. That's the whole thing. πŸ’…


🌸 SYNOPSIS 🌸

First things first: it lives in the header <utility>, so go say hi to it there.

template< class R, class T >              (since C++20)
constexpr bool in_range( T t ) noexcept;

Here's the tea πŸ“: it returns true if the value of t is in the range of values that can be represented in R β€” meaning t can be converted to R in a value-preserving manner. No sneaky truncation, no little gremlin data-loss moment. πŸ’€

Heads up bestie: it is a compile-time error if either T or U is a non-integer type, a character type, or bool. So don't try it. It will not be cute. πŸ™ˆ

πŸ¦‹ PARAMETERS πŸ¦‹

  • t β€” the value to test. That's it. That's the whole parameter list. She's low-maintenance πŸ‘‘

πŸŽ€ RETURN VALUE πŸŽ€

  • true if the value of t is representable in R
  • false otherwise

Binary queen. No in-between, no "it depends", no situationship. 🌈


πŸ‘‘ POSSIBLE IMPLEMENTATION πŸ‘‘

Want to see how the sausage gets made? Here's a possible implementation β€” not the only way, just a way:

template<class R, class T>
constexpr bool in_range(T t) noexcept
{
    return std::cmp_greater_equal(t, std::numeric_limits<R>::min()) &&
        std::cmp_less_equal(t, std::numeric_limits<R>::max());
}

Honestly? Iconic. It just asks "is t at least R's min AND at most R's max" β€” and it does it with the std::cmp_* functions so nothing overflows behind your back. We love a careful king 🫢


⭐ NOTES ⭐

Real talk for a sec β€” this function cannot be used with enums (including std::byte), char, char8_t, char16_t, char32_t, wchar_t and bool. So if you were about to try that, bestie, no. The compiler said no. 😭

Also, here's the feature-test macro situation, because you know I'd never gatekeep:

  • __cpp_lib_integer_comparison_functions β€” value 202002L, std (C++20), feature: Integer comparison functions

Use it to check at compile time whether these functions exist before you go feral with the integer comparisons. iykyk πŸ”₯


πŸ”₯ EXAMPLE πŸ”₯

// Run this code β€” go off, run it:

#include <iostream>
#include <utility>

int main()
{
    std::cout << std::boolalpha;

    std::cout << std::in_range<std::size_t>(-1) << '\n';
    std::cout << std::in_range<std::size_t>(42) << '\n';
}

πŸ’œ Output:

false
true

Not me being obsessed with the -1 β†’ false moment 😭 Like yes queen, -1 is NOT chillin' inside a std::size_t, and she clocked it immediately. The 42 one though? Welcome home bestie ✨


🧚 SEE ALSO 🧚

  • ranges::min (C++20) β€” returns the smaller of the given values (niebloid)
  • ranges::max (C++20) β€” returns the greater of the given values (niebloid)
  • ranges::clamp (C++20) β€” clamps a value between a pair of boundary values (niebloid)
  • lerp (C++20) β€” linear interpolation function (function)

All the range-y besties in one place πŸ“


and that's the page!! πŸ’– this little gem came to you from http://cppreference.com, dated 2024.06.10, and it's filed under std::in_range(3) in the C++ Standard Library manual β€” mwah, go forth and range-check responsibly πŸ«ΆπŸ’…βœ¨

πŸ“¦ where this page comes from