std::integral
std::integral (정수 타입 개념)
T가 정수 타입인지를 명세하는 개념(concept)이에요. std::is_integral_v<T>와 동일하게 동작해요. C++20부터 있어요.
출처: cppreference
본문
<concepts> 헤더에 정의돼 있어요.
template< class T >
concept integral = std::is_integral_v<T>;
개념 integral<T>는 T가 정수 타입일 때(그리고 그때만) 만족돼요. bool, char, int, 각 크기의 정수 타입 및 그 cv 한정 형태가 해당돼요.
예제를 보면 오버로드 선택에 활용할 수 있어요.
#include <concepts>
#include <iostream>
void print(std::integral auto i)
{
std::cout << "Integral: " << i << '\n';
}
void print(auto x)
{
std::cout << "Non-integral: " << x << '\n';
}
int main()
{
std::cout << std::boolalpha;
static_assert(std::integral<bool>);
print(true);
static_assert(std::integral<char>);
print('o');
static_assert(std::integral<int>);
print(007);
static_assert( ! std::integral<double> );
print(2e2);
static_assert( ! std::integral<decltype("")> );
print("∫∫∫");
}
출력:
Integral: true
Integral: o
Integral: 7
Non-integral: 200
Non-integral: ∫∫∫
이렇게 정수 타입에만 적용되는 제약된 함수 템플릿을 쉽게 작성할 수 있어요. std::floating_point 개념과 함께 산술 개념(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]