如何检查模板参数是否是std :: vector< T> :: iterator?
对于void类型,我们有std :: is_void.对于std :: vector< T> :: iterator有类似的东西吗?
解决方法
你可以为它创建一个特征:
#include <vector> #include <list> #include <type_traits> template <class T,class = void> struct is_vector_iterator: std::is_same<T,std::vector<bool>::iterator> { }; template <class T> struct is_vector_iterator<T,decltype(*std::declval<T>(),std::enable_if_t<!std::is_same<T,std::vector<bool>::iterator>::value>())>: std::is_same<T,typename std::vector<std::decay_t<decltype(*std::declval<T>())>>::iterator> { }; int main() { static_assert(is_vector_iterator<std::vector<int>::iterator>::value,"Is not a vector iterator"); static_assert(is_vector_iterator<std::vector<bool>::iterator>::value,"Is not a vector iterator"); static_assert(!is_vector_iterator<std::list<int>::iterator>::value,"Is a vector iterator"); static_assert(!is_vector_iterator<std::list<int>::iterator>::value,"Is a vector iterator"); }