命名空间
变体
操作

std::signed_integral (自 C++20 起)

来自 cppreference.com
< cpp‎ | concepts
定义在头文件 <concepts>
template< class T >
concept signed_integral = std::integral<T> && std::is_signed_v<T>;
(自 C++20 起)

仅当 T 是一个整型且 std::is_signed_v<T>true 时,概念 signed_integral<T> 才满足。

内容

[编辑] 注释

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)
检查一个类型是否为带符号算术类型
(类模板) [编辑]