一、类型萃取范例
1.通过萃取接口中的 value 值为true,false 我们就可以萃取出很多有用的信息。
#include <iostream>
#include <future>
#include <vector>
#include <map>
#include <functional>
#include <string>
#include <algorithm>
#include <ctime>
#include <initializer_list>
#include <stdarg.h>
#include <list>
using namespace std;
template <typename T>
void printTraitsInfo(const T& t)
{
cout << "我们要萃取的名字是 :" << typeid(T).name() << endl;
cout << "is_void = " << is_void<T>::value << endl; //类型是否是void
cout << "is_calss = " << is_class<T>::value << endl; //类型是否是class
cout << "is_object = " << is_object<T>::value << endl; //类型是否是object
cout << "is_pod = " << is_pod<T>::value << endl; // 是否是普通类(只包含成员变量,不包含成员函数)
cout << "is_default_constructible =" << is_default_constructible<T>::value << endl; // 缺省构造函数
cout << "is_copy_constructible =" << is_copy_constructible<T>::value << endl; // 拷贝构造函数
cout << "is_move_constructible =" << is_move_constructible<T>::value << endl; //移动构造函数
cout << "is_destructible = " << is_destructible<T>::value << endl; //析构函数
cout << "is_polymorphic = " << is_polymorphic<T>::value << endl; //虚函数
cout << "is_trivially_default_constructible = " << is_trivially_default_constructible<T>::value << endl; //缺省拷贝构造函数
cout << "has_virtual_destructor = " << has_virtual_destructor<T>::value << endl; //虚析构函数
}
class A
{
public:
A() = default;
A(A&& ta) = delete;
A(const A& ta) = delete;
virtual ~A() {}
};
class B
{
public:
int m_i;
int m_j;
};
class C
{
public:
C(int t) {}
};
void func()
{
printTraitsInfo(int()); //扔一个零食对象进去
printTraitsInfo(string());
printTraitsInfo(A());
printTraitsInfo(B());
printTraitsInfo(C(1));
printTraitsInfo(list<int>());
}
int main()
{
func();
return 0;
}