std::is_unsigned
From cppreference.com
| ヘッダ <type_traits> で定義 |
||
| template< class T > struct is_unsigned; |
(C++11以降) | |
std::is_unsigned は UnaryTypeTrait です。
T が符号なし算術型であるかどうかをチェックします。
- もし std::is_arithmetic<T>::value が true ならば、メンバ定数
valueは T(0) < T(-1) に等しい値を提供します。 - それ以外の場合、メンバ定数
valueは false に等しい値を提供します。
プログラムが std::is_unsigned または std::is_unsigned_v の特殊化を追加した場合、その動作は未定義です。
目次 |
[編集] テンプレートパラメータ
| T | - | チェックする型 |
[編集] ヘルパー変数テンプレート
| template< class T > constexpr bool is_unsigned_v = is_unsigned<T>::value; |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
T が符号なし整数型の場合は true、それ以外の場合は false(公開静的メンバ定数) |
メンバ関数
| operator bool |
オブジェクトを bool に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
bool |
type
|
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++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2197 | C++11 | value は T が算術型でなくても true になる可能性がある |
この場合、false しかありえない |
[編集] 関連項目
| (C++11) |
型が符号付き算術型であるかをチェックする (クラステンプレート) |
| [static] |
符号付きの型を識別する ( std::numeric_limits<T> の public static メンバ定数) |
| (C++11) |
型が算術型であるかをチェックする (クラステンプレート) |
| (C++11) |
与えられた整数型に対応する符号付き型を取得する (クラステンプレート) |
| (C++11) |
与えられた整数型に対応する符号付き型を取得する (クラステンプレート) |