4 Type Trait and Type Utility
1 Type Trait
type trait: Provide a treatment type Attribute approach ; It's a template, One can be generated during compilation based on one or more template arguments type or value;
* Definition and header file <type_traits> in ;
1.1 ( Single parameter ) Type judgment (TYPE predicate)
All types of tools :
(1) Used to test type properties Trait
(2). For testing class Type Trait
explain :
*
bool And all of them character type (char,char16_t,char32_t,wchar_t) Are treated as integer types ,std::nullptr_t Is considered the underlying data type ;
* One direction “const type ” The extraordinary quantity of pointer perhaps reference, It's not a constant , Even though it contains constant elements array It's a constant ;
example :
is_const<int>::value //false is_const<int* const> :: value //true , The address of the pointer cannot be modified
is_const<const int*>:: value //false , pointer to const , You can change the address is_const<int[]>::value
//false is_const<const int[]>::value //true
(3). Used to test type relations Trait
explain :
1.
is_assignable<> If the first type of is nonclass type , Will always get false_type, about class type , With its normal type as the first parameter, you can ;
example :
is_assignable<int,int>::value //false is_assignable<int&,int>::value //true
is_assignable<int&,void*>::value //false is_assignable<string,const char*>::
value //true
use is_constructible<>:
is_constructible<int>::value //true is_constructible<int,int>::value //true
is_constructible<int ,void*>::value //false
(4). type specifier
Allow type change , If the property does not exist , Or remove an existing property
// add to typedef int T; add_cosnt<T> ::type //const int add_pointer<T>::type
//int* // remove typeder int* T; remove_pointer<T>::type //int
(5). other Type Trait
2 Reference Wrapeer( Reference wrapper )
* Statement and <functional> Medium class std::reference_wrapper<>
*
For a given type T, This class provides two functions :
* ref(): hold T Implicitly convert to int&
* cref() : hold T Implicitly convert to const int&
example : template<typename T> void foo(T val); // After calling : int x; foo(std::ref(x));
//T Become int&, In the past : int x; foo(std::cref(x)); //T Become const int&
This feature is used everywhere :
make_pair(): Create a pair<> quote make_tuple() : Create a tuple<> quote Binder: For binding (bind) quote
Thread: with by reference Formal transfer arguments
【 be careful 】:reference_wrapper You can use reference types as top-level objects :
for example :
vector<Myclass&> cool; //error vector<reference_wrapper<Myclass> > cool; //ok
3 Function Type Wrapper( Function type wrapper )
* <functional> Provided in : function<>, Provide polymorphic wrapper
* this class Allow callable objects ( as function,member function, function object and lambda) As the highest level object ;
example :
void func(int x, int y); vector<function<void(int ,int)>> task;
task.push_back(func); task.push_back([](int x, int y ){...});//lambda function // Call function
for(function<void(int,int)> f: task) { f(33,66); } // use member function class C{
public: void memfunc(int x, in y)const; }; function<void(const C&,int,int)> mf;
mf = &C::memfunc; mf(C(),32,22);
【 be careful :】 Execute a function call : But there is no target to call , It will be thrown out std::bad_function_call abnormal
example :
function<void(int,int)> f; // Empty object f(33,33);//throw bad_function_call
Technology
Daily Recommendation