命名空间
变体
操作

std::in_range

来自 cppreference.cn
< cpp‎ | 工具
 
 
 
在头文件 <utility> 中定义
template< class R, class T >
constexpr bool in_range( T t ) noexcept;
(C++20 起)

如果 t 的值在 R 可表示的范围内,即 t 可以以保留值的方式转换为 R,则返回 true

如果 TU 是非整型类型、字符类型或 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)、charchar8_tchar16_tchar32_twchar_tbool 一起使用。

特性测试 标准 特性
__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)
线性插值函数
(函数) [编辑]