std::signed_integral
std::signed_integral (부호 있는 정수 타입 개념)
T가 부호 있는 정수 타입인지를 명세하는 개념(concept)이에요. std::integral이면서 std::is_signed_v<T>가 참이어야 해요. C++20부터 있어요.
출처: cppreference
본문
<concepts> 헤더에 정의돼 있어요.
template< class T >
concept signed_integral = std::integral<T> && std::is_signed_v<T>;
개념 signed_integral<T>는 T가 정수 타입이고 std::is_signed_v<T>가 true일 때(그리고 그때만) 만족돼요.
참고
signed_integral<T>가 반드시 "부호 있는 정수 타입"이 아닌 타입에 의해 만족될 수도 있어요. 예를 들어 char(char가 부호 있는 시스템에서)가 그렇죠. 실제로는 부호 표현(sign representation)이 있는 정수형이라면 만족할 수 있어요.
예제를 보면 오버로드 선택에 활용할 수 있어요.
#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
이렇게 부호 유무에 따라 다른 오버로드를 선택하는 데 쓰여요. std::unsigned_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]