std::in_range
來自 cppreference.com
在標頭檔案 <utility> 中定義 |
||
template< class R, class T > constexpr bool in_range( T t ) noexcept; |
(C++20 起) | |
如果 t 的值在 R
可表示的範圍內,即 t 可以以保留值的方式轉換為 R
,則返回 true。
如果 T
或 U
是非整型型別、字元型別或 bool,則會發生編譯時錯誤。
目錄 |
[編輯] 引數
t | - | 要測試的值 |
[編輯] 返回值
如果 t 的值可在 R
中表示,則為 true,否則為 false。
[編輯] 可能的實現
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()); } |
[編輯] 注意
此函式不能與列舉(包括 std::byte)、char、char8_t、char16_t、char32_t、wchar_t 和 bool 一起使用。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_integer_comparison_functions |
202002L |
(C++20) | 整數比較函式 |
[編輯] 示例
執行此程式碼
#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'; }
輸出
false true
[編輯] 參閱
(C++20) |
返回給定值中較小的那個 (演算法函式物件) |
(C++20) |
返回給定值中較大的那個 (演算法函式物件) |
(C++20) |
將值限制在邊界值對之間 (演算法函式物件) |
(C++20) |
線性插值函式 (函式) |