名稱空間
變體
操作

std::signed_integral

來自 cppreference.com
定義於標頭檔案 <concepts>
template< class T >
concept signed_integral = std::integral<T> && std::is_signed_v<T>;
(C++20 起)

概念 signed_integral<T> 被滿足當且僅當 T 是一個整型,並且 std::is_signed_v<T>true

目錄

[編輯] 注意

signed_integral<T> 可以被一個不是有符號整型的型別滿足,例如,char (在 char 為有符號的系統上)。

[編輯] 示例

#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

[編輯] 參考

  • C++23 標準 (ISO/IEC 14882:2024)
  • 18.4.7 算術概念 [concepts.arithmetic]
  • C++20 標準 (ISO/IEC 14882:2020)
  • 18.4.7 算術概念 [concepts.arithmetic]

[編輯] 參閱

檢查型別是否為整型
(類模板) [編輯]
(C++11)
檢查型別是否為有符號算術型別
(類模板) [編輯]