std::numeric_limits<T>::is_signed
From cppreference.com
static const bool is_signed; |
(C++11まで) | |
| static constexpr bool is_signed; |
(C++11以降) | |
T が符号付き算術型の場合は true、符号なし型の場合は false になります。この定数はすべての特殊化で意味を持ちます。
[編集] 標準特殊化
T
|
value of std::numeric_limits<T>::is_signed |
| /* 非特殊化 */ | false |
| bool | false |
| char | 実装定義 |
| signed char | true |
| unsigned char | false |
| wchar_t | 実装定義 |
| char8_t (C++20 以降) | false |
| char16_t (C++11 以降) | false |
| char32_t (C++11 以降) | false |
| short | true |
| unsigned short | false |
| int | true |
| unsigned int | false |
| long | true |
| unsigned long | false |
| long long (C++11 以降) | true |
| unsigned long long (C++11 以降) | false |
| float | true |
| double | true |
| long double | true |
[編集] 例
このコードを実行
#include <iostream> #include <iomanip> #include <limits> template<typename T> struct test { test(const char* name, int w = 15) { std::cout << std::left << std::setw(w) << (std::numeric_limits<T>::is_specialized ? name : "non-specialized") << " : " << (std::numeric_limits<T>::is_signed ? "" : "un") << "signed\n"; } }; int main() { test<bool>{"bool"}; test<char>{"char"}; test<wchar_t>{"wchar_t"}; test<char16_t>{"char16_t"}; test<char32_t>{"char32_t"}; test<float>{"float"}; struct delusion{}; test<delusion>{"delusion"}; test<decltype(42)>{"decltype(42)"}; }
実行結果の例
bool : unsigned char : signed wchar_t : signed char16_t : unsigned char32_t : unsigned float : signed non-specialized : unsigned decltype(42) : signed
[編集] 関連項目
| (C++11) |
型が符号付き算術型であるかをチェックする (クラステンプレート) |
| [static] |
整数型を識別する (public static member constant) |
| [static] |
厳密な型を識別する (public static member constant) |
| [static] |
有限の値の集合を表現する型を識別する (public static member constant) |