C++ 模板学习 实现简易的类型萃取

得益于C++ 模板的特化和SFINAE特性 我们可以实现自己的类型萃取。

个人认为比较有意思的例子,通过宏定义和模板来限制模板参数的类型,

#pragma once
#define is__stl_type__(T) T, typename = typename my_enable_if<is_stl<T>::value>::type
#define is__Sequential_stl_type__(T) T, typename = typename my_enable_if<is_Sequential_stl<T>::value>::type

template<bool type_b, class type_t = void>
struct my_enable_if;

template<class type_t>
struct my_enable_if<true, type_t> {
	using type = type_t;
};


template<class T>
struct void_type {
	using type = void;
};

template<bool b>
struct bool_type {
	static const bool value = b;
};

template<class T, class u = void>
struct has_iterator : bool_type<false> {

};

template<class T>
struct has_iterator<T, typename void_type<typename T::iterator>::type> :bool_type<true> {
	using type = void;
};

template<class T, class = void>
struct has_begin :bool_type<false> {};

template<class T>
struct has_begin<T, typename void_type<typename decltype(T().begin())>::type> : bool_type<true> {};

template<class T, class = void>
struct has_end :bool_type<false> {};

template<class T>
struct has_end<T, typename void_type<typename decltype(T().end())>::type> : bool_type<true> {};

template<class T>
struct is_stl {
	static constexpr bool value = has_iterator<T>::value;
};

template<class T>
struct is_Sequential_stl {
	static constexpr bool value = 
		has_iterator<T>::value &&
		has_begin<T>::value &&
		has_end<T>::value;
};

个人认为GP是C++ 最为有意思的一部分

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值