首页 > 代码库 > meta program (1) swap_iter

meta program (1) swap_iter

开始学习模板元编程了

#include <iostream>
#include <string>
#include <vector>
#include <list>

#include "boost/assign/list_of.hpp"
#include "boost/type_traits/is_const.hpp"
#include "boost/type_traits/is_reference.hpp"
#include "boost/type_traits/is_same.hpp"

#include "boost/shared_ptr.hpp"

namespace swap_test
{
	template<bool use_swap> struct use_swap_impl;

	template<typename ForwardIterator1, typename ForwardIterator2>
	static void swap_iter(ForwardIterator1& i1, ForwardIterator2& i2)
	{
		typedef std::iterator_traits<ForwardIterator1> trait1;
		typedef typename trait1::value_type v1;
		typedef typename trait1::reference r1;

		typedef std::iterator_traits<ForwardIterator2> trait2;
		typedef typename trait2::value_type v2;
		typedef typename trait2::reference r2;

		bool const use_swap = boost::is_same<v1, v2>::value 
			&& boost::is_reference<r1>::value
			&& boost::is_reference<r2>::value;//防止代理引用vector<bool>的迭代器,见mpl page30.

		use_swap_impl<use_swap>::to_do_swap(i1, i2);
	}

	template<>//两种类型是一样的,
	struct use_swap_impl<true>
	{
		template<typename ForwardIterator1, typename ForwardIterator2>
		static void to_do_swap(ForwardIterator1& i1, ForwardIterator2& i2)
		{
			std::swap(*i1, *i2);
		}
	};

	template<>
	struct use_swap_impl<false>
	{
		template<typename ForwardIterator1, typename ForwardIterator2>
		static void to_do_swap(ForwardIterator1& i1, ForwardIterator2& i2)
		{
			typename std::iterator_traits<ForwardIterator1>::value_type
			tmp = *i1;
			*i1 = *i2;
			*i2 = tmp;
		}
	};

	void fun()
	{
		std::list<std::string> list = boost::assign::list_of("123")("456");
		std::vector<std::string> vec = boost::assign::list_of("789")("013");

		swap_iter(list.begin(), vec.begin());
		std::cout << *(list.begin()) << " " << *(vec.begin()) << std::endl;

		boost::shared_ptr<int> t1(new int(18));
		boost::shared_ptr<float> t2(new float(20.0f));

		int *tt1 = t1.get();
		float *tt2 = t2.get();
		swap_tier(tt1, tt2);
		std::cout << *t1 << " " << *t2 << std::endl;
	}
}