2_9_10 可变参函数、initializer_list、萃取

9_10_可变参函数_initializer_list.cpp

#include "hjcommon.hpp"

class A
{
public:
	A() { cout << "A no arg." << endl; }
	A(const A &a) { cout << "A copy." << endl; }
	~A() { cout << "A destructor." << endl; }
};

static void aFunc(initializer_list<A> ls1, int num=0) {} // c++11 可变参函数

int main_2_9_10()
{
	// c++11 新标准可变参函数,使用 initializer_list<> ,前提需要所有参数数据类型相同, 可包含 #include <initializer_list>
	initializer_list<A> ls1 = { A(), A(), A() }; // 可理解为数组,容器,注意元素值是常量值
	// initializer_list 发生拷贝时,不会拷贝其中的元素, 初始化 initializer_list 时,也不会拷贝元素值到 initializer_list 。。
	aFunc(ls1);

	// 省略号形参(...) c语言风格,一般无法正常解析c++的类类型,不建议在 c++ 中使用。

	return 0;
}

9_11_traits.cpp

#include "hjcommon.hpp"
#include <list>

template <typename T>
static void printTraitsInfo(const T& t)
{
	cout << "--------------begin-----------" << endl;
	cout << "萃取的类型名:" << ((const type_info&)typeid(T)).name() << endl;
	cout << "is_void = " << is_void<T>::value << endl;                                    // 是否是void
	cout << "is_class = " << is_class<T>::value << endl;                                  // 是否是一个class
	cout << "is_object = " << is_object<T>::value << endl;                                // 是否是一个对象类型
	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) {}
};

int main()
{
	// traits 类型萃取,都是类模板,属于泛型编程,c++11引入
	// https://en.cppreference.com/w/cpp/types    萃取接口查看

	/* 打印:
			--------------begin-----------
			萃取的类型名:i
			is_void = 0												// 是否是void
			is_class = 0											// 是否是一个class
			is_object = 1											// 是否是一个对象类型
			is_pod = 1												// 是否普通类(只包含成员变量,不含成员函数)
			is_default_constructible = 1							// 是否有缺省构造函数
			is_copy_constructible = 1								// 是否有拷贝构造函数
			is_move_constructible = 1								// 是否有移动构造函数
			is_destructible = 1										// 是否有析沟函数
			is_polymorphic = 0										// 是否有虚函数
			is_trivially_default_constructible = 1					// 缺省拷贝构造函数是否是可有可无的
			has_virtual_destructor = 0								// 是否有虚析沟函数
			--------------begin-----------
			萃取的类型名:NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
			is_void = 0
			is_class = 1
			is_object = 1
			is_pod = 0
			is_default_constructible = 1
			is_copy_constructible = 1
			is_move_constructible = 1
			is_destructible = 1
			is_polymorphic = 0
			is_trivially_default_constructible = 0
			has_virtual_destructor = 0
			--------------begin-----------
			萃取的类型名:1A
			is_void = 0
			is_class = 1
			is_object = 1
			is_pod = 0
			is_default_constructible = 1
			is_copy_constructible = 0
			is_move_constructible = 0
			is_destructible = 1
			is_polymorphic = 1
			is_trivially_default_constructible = 0
			has_virtual_destructor = 1
			--------------begin-----------
			萃取的类型名:1B
			is_void = 0
			is_class = 1
			is_object = 1
			is_pod = 1
			is_default_constructible = 1
			is_copy_constructible = 1
			is_move_constructible = 1
			is_destructible = 1
			is_polymorphic = 0
			is_trivially_default_constructible = 1
			has_virtual_destructor = 0
			--------------begin-----------
			萃取的类型名:1C
			is_void = 0
			is_class = 1
			is_object = 1
			is_pod = 0
			is_default_constructible = 0
			is_copy_constructible = 1
			is_move_constructible = 1
			is_destructible = 1
			is_polymorphic = 0
			is_trivially_default_constructible = 0
			has_virtual_destructor = 0
			--------------begin-----------
			萃取的类型名:NSt7__cxx114listIiSaIiEEE
			is_void = 0
			is_class = 1
			is_object = 1
			is_pod = 0
			is_default_constructible = 1
			is_copy_constructible = 1
			is_move_constructible = 1
			is_destructible = 1
			is_polymorphic = 0
			is_trivially_default_constructible = 0
			has_virtual_destructor = 0
	 */
	printTraitsInfo(int());
	printTraitsInfo(string());
	printTraitsInfo(A());
	printTraitsInfo(B());
	printTraitsInfo(C(1));
	printTraitsInfo(list<int>());

	// 模板与泛型编程,模板元编程

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值