std::floating_point
std::floating_point (부동소수점 타입 개념)
T가 부동소수점 타입인지를 명세하는 개념(concept)이에요. std::is_floating_point_v<T>와 동일하게 동작해요. C++20부터 있어요.
출처: cppreference
본문
<concepts> 헤더에 정의돼 있어요.
template< class T >
concept floating_point = std::is_floating_point_v<T>;
개념 floating_point<T>는 T가 부동소수점 타입일 때(그리고 그때만) 만족돼요. 즉 float, double, long double 및 그 cv 한정 형태가 해당돼요.
예제를 보면 오버로드 선택에 활용할 수 있어요.
#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
부동소수점 인자에는 더하는 오버로드가, 정수 인자에는 비트 이동 오버로드가 선택돼요. std::integral 개념과 함께 산술 개념(arithmetic concepts) 계열을 이뤄요.
참고 문헌
- C++23 표준 (ISO/IEC 14882:2024): 18.4.7 Arithmetic concepts [concepts.arithmetic]
- C++20 표준 (ISO/IEC 14882:2020): 18.4.7 Arithmetic concepts [concepts.arithmetic]