std::is_pointer
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct is_pointer; |
(C++11 起) | |
std::is_pointer
是一個 一元型別特性。
檢查 T
是否為物件或函式指標(包括指向 void 的指標,但不包括指向成員的指標)或其 cv 限定版本。提供成員常量 value
,如果 T
是物件/函式指標型別,則其等於 true。否則,value
等於 false。
如果程式為 std::is_pointer
或 std::is_pointer_v
新增特化,則行為未定義。
目錄 |
[編輯] 模板引數
T | - | 要檢查的型別 |
[編輯] 輔助變數模板
template< class T > constexpr bool is_pointer_v = is_pointer<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> |
[編輯部分:可能的實現] 可能的實現
template<class T> struct is_pointer : std::false_type {}; template<class T> struct is_pointer<T*> : std::true_type {}; template<class T> struct is_pointer<T* const> : std::true_type {}; template<class T> struct is_pointer<T* volatile> : std::true_type {}; template<class T> struct is_pointer<T* const volatile> : std::true_type {}; |
[編輯部分:示例] 示例
執行此程式碼
#include <type_traits> int main() { struct A { int m; void f() {} }; int A::*mem_data_ptr = &A::m; // a pointer to member data void (A::*mem_fun_ptr)() = &A::f; // a pointer to member function static_assert( ! std::is_pointer<A>::value && ! std::is_pointer_v<A> // same thing as above, but in C++17! && ! std::is_pointer<A>() // same as above, using inherited operator bool && ! std::is_pointer<A>{} // ditto && ! std::is_pointer<A>()() // same as above, using inherited operator() && ! std::is_pointer<A>{}() // ditto && std::is_pointer_v<A*> && std::is_pointer_v<A const* volatile> && ! std::is_pointer_v<A&> && ! std::is_pointer_v<decltype(mem_data_ptr)> && ! std::is_pointer_v<decltype(mem_fun_ptr)> && std::is_pointer_v<void*> && ! std::is_pointer_v<int> && std::is_pointer_v<int*> && std::is_pointer_v<int**> && ! std::is_pointer_v<int[10]> && ! std::is_pointer_v<std::nullptr_t> && std::is_pointer_v<void (*)()> ); }
[編輯部分:另請參見] 另請參見
(C++11) |
檢查型別是否為指向非靜態成員函式或物件的指標 (類模板) |
(C++11) |
檢查型別是否為非靜態成員物件指標 (類模板) |
(C++11) |
檢查型別是否為非靜態成員函式指標 (類模板) |
(C++11) |
檢查型別是否為陣列型別 (類模板) |
(C++11) |
檢查型別是否為標量型別 (類模板) |