名稱空間
變體
操作

std::is_unsigned

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
is_unsigned
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <type_traits>
template< class T >
struct is_unsigned;
(C++11 起)

std::is_unsigned 是一個 UnaryTypeTrait

檢查 T 是否為無符號算術型別。

  • std::is_arithmetic<T>::valuetrue,則提供成員常量 value 等於 T(0) < T(-1)
  • 否則,提供成員常量 value 等於 false

若程式為 std::is_unsignedstd::is_unsigned_v 新增特化,則行為未定義。

目錄

[編輯] 模板引數

T - 要檢查的型別

[編輯] 輔助變數模板

template< class T >
constexpr bool is_unsigned_v = is_unsigned<T>::value;
(C++17 起)

繼承自 std::integral_constant

成員常量

value
[靜態]
T 是無符號整型則為 true,否則為 false
(public static 成員常量)

成員函式

operator bool
將物件轉換為 bool,返回 value
(公開成員函式)
operator()
(C++14)
返回 value
(公開成員函式)

成員型別

型別 定義
value_type bool
型別 std::integral_constant<bool, value>

[編輯] 可能實現

namespace detail
{
    template<typename T,bool = std::is_arithmetic<T>::value>
    struct is_unsigned : std::integral_constant<bool, T(0) < T(-1)> {};
 
    template<typename T>
    struct is_unsigned<T,false> : std::false_type {};
} // namespace detail
 
template<typename T>
struct is_unsigned : detail::is_unsigned<T>::type {};

[編輯] 示例

#include <iostream>
#include <type_traits>
 
class A {};
static_assert(std::is_unsigned_v<A> == false);
 
enum B : unsigned {};
static_assert(std::is_unsigned_v<B> == false);
 
enum class C : unsigned {};
static_assert(std::is_unsigned_v<C> == false);
 
struct S { unsigned p : 1; int q : 1; };
static_assert
(
    std::is_unsigned_v<decltype(S::p)> not_eq
    std::is_unsigned_v<decltype(S::q)>
);
 
static_assert
(
    std::is_unsigned_v<float> == false &&
    std::is_unsigned_v<signed int> == false &&
    std::is_unsigned_v<unsigned int> == true &&
    std::is_unsigned_v<bool> == true
);
 
int main() 
{
    // signedness of char is implementation-defined:
    std::cout << std::boolalpha << std::is_unsigned<char>::value << '\n';
}

可能的輸出

false

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2197 C++11 即使 T 不是算術型別,value 也可能為 true 在此情況下只能為 false

[編輯] 參見

(C++11)
檢查型別是否為有符號算術型別
(類模板) [編輯]
[靜態]
確定有符號型別
(std::numeric_limits<T> 的公共靜態成員常量) [編輯]
檢查型別是否為算術型別
(類模板) [編輯]
獲取給定整型對應的有符號型別
(類模板) [編輯]
獲取給定整型對應的有符號型別
(類模板) [編輯]