类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。
试图特化定义于
定义于
定义于头文件
| value [静态] | 若 T 为指针类型则为 true ,否则为 false(公开静态成员常量) |
| operator bool | 转换对象为 bool ,返回 value(公开成员函数) |
| operator() (C++14) | 返回 value(公开成员函数) |
| 类型 | 定义 |
value_type | bool |
type | std::integral_constant |
std::is_pointer
| template< class T > | (C++11 起) |
检查 T 是否为指向对象指针或指向函数指针(但不是指向成员/成员函数指针)。若 T 是对象/函数指针类型,则提供等于 true 的成员常量 value 。否则, value 等于 false 。
| T | - | 要检查的类型 |
| template< class T > | (C++17 起) |
- template< class T > struct is_pointer_helper : std::false_type {};
- template< class T > struct is_pointer_helper<T*> : std::true_type {};
- template< class T > struct is_pointer :
- is_pointer_helper<typename std::remove_cv<T>::type> {};
std::is_lvalue_reference
| template< class T > | (C++11 起) |
检查 T 是否为左值引用类型。若 T 是左值引用类型,则提供等于 true 的成员常量 value ,否则, value 等于 false 。
| T | - | 要检查的类型 |
| template< class T > | (C++17 起) |
- template<class T> struct is_lvalue_reference : std::false_type {};
- template<class T> struct is_lvalue_reference<T&> : std::true_type {};
std::is_rvalue_reference
| template< class T > | (C++11 起) |
检查 T 是否为右值引用类型。若 T 是右值引用类型,则提供等于 true 的成员常量 value ,否则, value 等于 false 。
| T | - | 要检查的类型 |
| template< class T > | (C++17 起) |
- template <class T> struct is_rvalue_reference : std::false_type {};
- template <class T> struct is_rvalue_reference<T&&> : std::true_type {};
- #include <iostream>
- #include <type_traits>
-
- class A {};
-
- int main()
- {
- std::cout << std::boolalpha;
- << std::is_pointer<A>::value << std::endl;
- << std::is_pointer<A *>::value << std::endl;
- << std::is_pointer<A &>::value << std::endl;
- std::cout << "std::is_pointer
::value: " - << std::is_pointer<int>::value << std::endl;
- std::cout << "std::is_pointer
::value: " - << std::is_pointer<int *>::value << std::endl;
- std::cout << "std::is_pointer
::value: " - << std::is_pointer<int **>::value << std::endl;
- std::cout << "std::is_pointer
::value: " - << std::is_pointer<int[10]>::value << std::endl;
- std::cout << "std::is_pointer
::value:" - << std::is_pointer<std::nullptr_t>::value << std::endl;
- std::cout << std::endl;
-
- << std::is_lvalue_reference<A>::value << std::endl;
- << std::is_lvalue_reference<A&>::value << std::endl;
- << std::is_lvalue_reference < A&& >::value << std::endl;
- std::cout << "std::is_lvalue_reference
::value: " - << std::is_lvalue_reference<int>::value << std::endl;
- std::cout << "std::is_lvalue_reference
::value: " - << std::is_lvalue_reference<int&>::value << std::endl;
- std::cout << "std::is_lvalue_reference
::value: " - << std::is_lvalue_reference < int&& >::value << std::endl;
- std::cout << std::endl;
-
- << std::is_rvalue_reference<A>::value << std::endl;
- << std::is_rvalue_reference<A&>::value << std::endl;
- << std::is_rvalue_reference < A&& >::value << std::endl;
- std::cout << "std::is_rvalue_reference
::value: " - << std::is_rvalue_reference<int>::value << std::endl;
- std::cout << "std::is_rvalue_reference
::value: " - << std::is_rvalue_reference<int&>::value << std::endl;
- std::cout << "std::is_rvalue_reference
::value: " - << std::is_rvalue_reference < int&& >::value << std::endl;
- std::cout << std::endl;
-
- return 0;
- }
