std::in_range
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
in_range
(C++20)
Defined in header
<utility>
template< class R, class T >
constexpr bool in_range( T t ) noexcept;
(since C++20)
constexpr bool in_range( T t ) noexcept;
Returns true if the value of t is in the range of values that can be represented in R
, that is, if t can be converted to R
in a value-preserving manner.
It is a compile-time error if either T
or U
is a non-integer type, a character type, or bool.
[edit] Parameters
t
-
value to test
[edit] Return value
true if the value of t is representable in R
, false otherwise.
[edit] Possible implementation
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()); }
[edit] Notes
This function cannot be used with enums (including std::byte), char, char8_t, char16_t, char32_t, wchar_t and bool.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_integer_comparison_functions |
202002L |
(C++20) | Integer comparison functions |
[edit] Example
Run this code
#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