得益于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++ 最为有意思的一部分