std::floating_point

来自cppreference.com
< cpp‎ | concepts
在标头 <concepts> 定义
template < class T >
concept floating_point = std::is_floating_point_v<T>;
(C++20 起)

概念 floating_point<T> 当且仅当 T 为浮点类型时得到满足。

示例

#include <concepts>
#include <iostream>
#include <type_traits>
 
constexpr std::floating_point auto x2(std::floating_point auto x) {
    return x + x;
}
 
constexpr std::integral auto x2(std::integral auto x) {
    return x << 1;
}
 
int main() {
    constexpr auto d = x2(1.1);
    static_assert(std::is_same_v<double const, decltype(d)>);
    std::cout << d << '\n';
 
    constexpr auto f = x2(2.2f);
    static_assert(std::is_same_v<float const, decltype(f)>);
    std::cout << f << '\n';
 
    constexpr auto i = x2(444);
    static_assert(std::is_same_v<int const, decltype(i)>);
    std::cout << i << '\n';
}

输出:

2.2
4.4
888

参阅

检查类型是否是浮点类型
(类模板)