std::is_member_pointer
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct is_member_pointer; |
(C++11 起) | |
std::is_member_pointer
是一個 一元型別特性 (UnaryTypeTrait)。
如果 T
是指向非靜態成員物件的指標或指向非靜態成員函式的指標,則提供成員常量 value 等於 true。對於任何其他型別,value 為 false。
如果程式為 std::is_member_pointer
或 std::is_member_pointer_v
新增特化,則行為未定義。
目錄 |
[編輯] 模板引數
T | - | 要檢查的型別 |
[編輯] 輔助變數模板
template< class T > constexpr bool is_member_pointer_v = is_member_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_member_pointer_helper : std::false_type {}; template<class T, class U> struct is_member_pointer_helper<T U::*> : std::true_type {}; template<class T> struct is_member_pointer : is_member_pointer_helper<typename std::remove_cv<T>::type> {}; |
[編輯] 示例
執行此程式碼
#include <cassert> #include <type_traits> static_assert(!std::is_member_pointer_v<int*>); struct S { int i{42}; int foo() { return 0xF00; } }; using mem_int_ptr_t = int S::*; using mem_fun_ptr_t = int (S::*)(); static_assert(std::is_member_pointer_v<mem_int_ptr_t>); static_assert(std::is_member_pointer_v<mem_fun_ptr_t>); int main() { S s; mem_int_ptr_t pm = &S::i; assert(s.i == s.*pm); mem_fun_ptr_t pmf = &S::foo; assert((s.*pmf)() == s.foo()); }
[編輯] 參閱
(C++11) |
檢查型別是否為指標型別 (類模板) |
(C++11) |
檢查型別是否為非靜態成員物件指標 (類模板) |
(C++11) |
檢查型別是否為非靜態成員函式指標 (類模板) |