boost.core.enable_if

//made by davidsu33
//boost.core.enable_if

#include "stdafx.h"

#include <boost/core/enable_if.hpp>
#include <boost/units/io.hpp>

template <class T>
struct Test
{
	typename T TypeName;
};

void test_enableif()
{
	//enable_if<Cond, T>的用法是如果条件成立,则推导出类型T作为type
	//否则由于enable_if_c<false> 没有定义结构体的type会导致编译不过去
	//template <bool B, class T = void>
	//struct enable_if_c {
	//	typedef T type;
	//};

	//template <class T>
	//struct enable_if_c<false, T> {};

	//template <class Cond, class T = void> 
	//struct enable_if : public enable_if_c<Cond::value, T> {};
	
	typedef boost::enable_if<boost::is_integral<int>, unsigned>::type T;
	//typedef boost::enable_if<boost::is_integral<double>, unsigned>::type TM; //由于Cond错误编译不过
	T x;
	BOOST_STATIC_ASSERT(boost::is_same<T, unsigned>::value);

	typedef boost::disable_if<boost::is_integral<double>, char>::type T2;
	//typedef boost::disable_if<boost::is_integral<int>, char>::type TM2;//条件不满足编译不过
	T2 y;
	BOOST_STATIC_ASSERT(boost::is_same<T2, char>::value);

	//template <bool B, class T>
	//struct lazy_enable_if_c {
	//	typedef typename T::type type;
	//};

	//lazy用法和普通的enable (disable)的用法差异在于
	//lazy推导的时候T不是直接类型而是T::type类型
	//而enable_if disable_if等也是推导的中间类型,所以
	//可以写出如下的代码

	BOOST_STATIC_ASSERT(
	boost::is_same<
		boost::lazy_enable_if<
		boost::is_float<double>,
		boost::enable_if<boost::is_same<int, int>, double>
		>::type
		,
		double>::value
		);
	//由于boost::enable_if<boost::is_same<int, int>, double>推导出的结果
	//::type才是最后的类型,所以可以使用lazy再次取出其类型
	//BOOST还是很变态的,牛逼
}

int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

你可能感兴趣的:(boost.enable_if)