std::unsigned_integral
std::unsigned_integral (부호 없는 정수 타입 개념)
T가 부호 없는 정수 타입인지를 명세하는 개념(concept)이에요. std::integral이면서 std::signed_integral이 아니어야 해요. C++20부터 있어요.
출처: cppreference
본문
<concepts> 헤더에 정의돼 있어요.
template< class T >
concept unsigned_integral = std::integral<T> && !std::signed_integral<T>;
개념 unsigned_integral<T>는 T가 정수 타입이고 std::is_signed_v<T>가 false일 때(그리고 그때만) 만족돼요.
참고
unsigned_integral<T>가 반드시 "부호 없는 정수 타입"이 아닌 타입에 의해 만족될 수도 있어요. 예를 들어 bool이 그렇죠. bool은 부호가 없으므로 이 개념을 만족해요.
예제를 보면 부호 유무에 따라 다른 오버로드를 선택할 수 있어요.
#include <concepts>
#include <iostream>
#include <string_view>
void test(std::signed_integral auto x, std::string_view text = "")
{
std::cout << text << " (" + (text == "") << x << ") is a signed integral\n";
}
void test(std::unsigned_integral auto x, std::string_view text = "")
{
std::cout << text << " (" + (text == "") << x << ") is an unsigned integral\n";
}
void test(auto x, std::string_view text = "")
{
std::cout << text << " (" + (text == "") << x << ") is non-integral\n";
}
int main()
{
test(42); // signed
test(0xFULL, "0xFULL"); // unsigned
test('A'); // platform-dependent
test(true, "true"); // unsigned
test(4e-2, "4e-2"); // non-integral (hex-float)
test("∫∫"); // non-integral
}
가능한 출력:
(42) is a signed integral
0xFULL (15) is an unsigned integral
(A) is a signed integral
true (1) is an unsigned integral
4e-2 (0.04) is non-integral
(∫∫) is non-integral
bool은 부호 없는 정수로 분류되는 걸 볼 수 있어요. std::signed_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]