STL源码解析模版

模版语法介绍

Posted by chensong on 2020-06-21 23::34::36

前言

正文

C++中模版语法解

1, 模版参数做特殊化设计

#include <iostream>


template<class T, class O>
class ctest_class
{
public:
	ctest_class() 
	{
		std::cout << "I, O" << std::endl;
	}
	//	static int m_data;
};
//特殊化处理
template<class T>
class ctest_class<T*, T*>
{
public:
	ctest_class()
	{
		std::cout << "T*, T*" << std::endl;
	}
};

//特殊化处理
template<class T>
class ctest_class<const T*, T*>
{
public:
	ctest_class()
	{
		std::cout << "const T* , T*" << std::endl;
	}
};



int main(int argc, char *argv[])
{
	ctest_class<int, char> obj1; // T, O
	ctest_class<int*, int *> obj2; // T*, T*
	ctest_class<const int*, int*> obj3;// const T*, T*
	system("puase");
	return EXIT_SUCCESS;
}

2, 内部没有用到这一常量定义

成员( members)函数使用template

 template<class T>
class ccalloc {};

template<class T, class Alloc = ccalloc>
class cvertor 
{
public:
	typedef T value_type;
	typedef value_type* iterator;

	template<class I>
	void insert(iterator position, I first, I last)
	{
		std::cout << "insert()" << std::endl;
	}
};

int main(int argc, char *argv[])
{

	{
		int la[5] = { 1, 3, 4,5, 6 };
		cvertor<int> x;
		cvertor<int>::iterator iter = NULL;
		x.insert(iter, la, la + 5);
	}

	system("puase");
	return EXIT_SUCCESS;
}

3, template参数可根据前一个template参数而设定默认值

class ccalloc {};


template<class T, class Alloc = ccalloc, size_t buffsize = 0>
class cdeque
{
public:
	cdeque()
	{
		std::cout << "cdeque" << std::endl;
	}
};

template<class T, class csequence = cdeque<T>>
class cstatck
{
public:
	cstatck()
	{
		std::cout << "cstatck " << std::endl;
	}
private:
	csequence m_data;
};


int main(int argc, char *argv[])
{
	{
		cstatck<int> x;

	}
	system("puase");
	return EXIT_SUCCESS;
}

4, class template 可否拥有non-type template 参数


class ccalloc {};
inline size_t __deque_buf_size(size_t n, size_t sz)
{
	return n != 0 ? n : (sz < 512 ? size_t(512 / sz) : size_t(1));
}


template<class T, class ref, class ptr, size_t bufsize>
struct  __deque_iterator
{
	typedef __deque_iterator<T, T&, T*, bufsize> iterator;
	typedef __deque_iterator<T, const T&, const T*, bufsize> const_iterator;
	static size_t buffer_size()
	{
		return __deque_buf_size(bufsize, sizeof(T));
	}
};

template<class T, class  Alloc = ccalloc, size_t buffsize = 0>
class ccdeque
{
public:
	typedef __deque_iterator<T, T&, T*, buffsize> iterator;
};


int main(int argc, char *argv[])
{
	{
	
		std::cout << ccdeque<int>::iterator::buffer_size() << std::endl;
		std::cout << ccdeque<int, ccalloc, 64>::iterator::buffer_size() << std::endl;
	}

	system("puase");
	return EXIT_SUCCESS;
}

5, 模版特化


class ccalloc {};
template<class T, class Alloc = ccalloc, size_t buffsize = 0>
class cdeque
{
public:
	cdeque()
	{
		std::cout << "cdeque" << std::endl;
	}
};

//
template<class T, class sequence = cdeque<T>>
class statck
{
public:
	// friend 有问题????
	//friend bool operator<<T>(const statck<T, sequence>&, const statck<T, sequence>&);
	////或者
	//friend bool operator==<T>(const statck&, const statck&);
	//friend bool operator<<T>(const statck&, const statck&);
	////或者
	//friend bool operator==<>(const statck&, const statck&);
	//friend bool operator< <>(const statck&, const statck&);
public:
	statck()
	{
		std::cout << "stack " << std::endl;
	}
private:
	sequence m_data;
};
//

template<class T, class sequence>
bool operator==(const statck<T, sequence>&x, const statck<T, sequence>&y)
{
	std::cout << "operator == " << std::endl;
	return true;
}
//template<class _Ty,
//	class _Container> inline
//	bool operator==(const stack<_Ty, _Container>& _Left,
//		const stack<_Ty, _Container>& _Right)
//{	// test for stack equality
//	return (_Left._Get_container() == _Right._Get_container());
//}

//template<class T, class sequence>
//bool operator==(const statck<T, sequence>&x, const statck<T, sequence>&y)
//{
//	std::cout << "operator ==" << std::endl;
//	return true;
//}
////
//template<class T, class sequence>
//bool operator<(const statck<T, sequence>&x, statck<T, sequence>&y)
//{
//	std::cout << "operator <" << std::endl;
//	return true;
//}

int main(int argc, char *argv[])
{/*
	ctest_class<int, char> obj1;
	ctest_class<int*, int *> obj2;
	ctest_class<const int*, int*> obj3;

	{
		int la[5] = { 1, 3, 4,5, 6 };
		cvertor<int> x;
		cvertor<int>::iterator iter = NULL;
		x.insert(iter, la, la + 5);
	}

	{
		cstatck<int> x;

	}



	{
	
		std::cout << ccdeque<int>::iterator::buffer_size() << std::endl;
		std::cout << ccdeque<int, ccalloc, 64>::iterator::buffer_size() << std::endl;
	}*/
	{
	
	/*std::stack < int> x1;
	std::stack<int> x2;*/
	//	x1 == x2;
		statck<int> x;
		statck<int> y;
		x == y;
	//	//x < y;

	//	statck<char > y1;
	}
	system("puase");
	return EXIT_SUCCESS;
}

结语