std::in_range(3)
π 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 eitherTorUis a non-integer type, a character type, orbool. 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 π
trueif the value oftis representable inRfalseotherwise
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β value202002L, 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms