Hak*_*hin 6 c++ iterator enable-if c++11
我需要学习如何使用enable_if.为此,我需要使用enable_if重新实现distance函数.我试过这个:
#include <iostream>
#include <vector>
#include <list>
#include <utility>
#include <type_traits>
template<class In>
typename std::enable_if<!std::is_random_acces_iterator<In>::value, std::iterator_traits<In>::difference_type>::type my_distance(In begin, In end, std::input_iterator_tag dummy){
typename std::iterator_traits<In>::difference_type n = 0;
while(begin!=end){
++begin; ++n;
}
std::cout << "STEPPING" << std::endl;
return n;
}
template<class Ran>
typename std::enable_if<std::is_random_acces_iterator<Ran>::value, std::iterator_traits<In>::difference_type>::type my_distance(Ran begin, Ran end, std::random_access_iterator_tag dummy){
std::cout << "RANDOM" << std::endl;
return end - begin;
}
template <class I> inline
typename std::iterator_traits<I>::difference_type my_distance_wrapper(I begin, I end){
typedef typename std::iterator_traits<I>::iterator_category cat;
return my_distance(begin, end, cat());
}
int main() {
std::vector<int> ve;
std::list<int> li;
for(int i = 0; i < 3; i++){
ve.push_back(i);
li.push_back(i);
}
std::cout << my_distance_wrapper(ve.begin(), ve.end()) << std::endl;
std::cout << my_distance_wrapper(li.begin(), li.end()) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想我可以通过使用std::is_random_acces_iterator<In>::value类似于std::is_pod<In>::value
我检查type_traits的一些函数来做到这一点但是找不到任何东西来检查某些东西是否是特定的迭代器.我如何检查是否有random_acces_iterator?我知道我可以在一个函数中执行此操作:
template<class T>
bool f(){
typedef typename std::iterator_traits<T>::iterator_category cat;
return std::random_access_iterator_tag == cat();
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题基本上是如何在模板中执行函数f?不能不使用enable_if是不可能的,这是我的任务的要求.我是否正确相信SFINAE会正确地丢弃其他函数,如果我可以将函数f放入该模板中?
您可以std::is_same<>按如下方式使用类型特征
template<typename iterator>
using is_random_access_iterator =
std::is_same<typename std::iterator_traits<iterator>::iterator_category,
std::random_access_iterator_tag>;
Run Code Online (Sandbox Code Playgroud)
然后
template<typename iterator>
std::enable_if_t<is_random_access_iterator<iterator>::value,
typename std::iterator_traits<iterator>::difference_type>
my_distance(iterator a, iterator b) { return a-b; }
template<typename iterator>
std::enable_if_t<!is_random_access_iterator<iterator>::value,
typename std::iterator_traits<iterator>::difference_type>
my_distance(iterator a, iterator b) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
这里,std::enable_if_t<,>是一个帮助器别名(从C++ 14开始)定义为
template<bool condition, typename type = void>
using enable_if_t = typename enable_if<condition,type>::type;
Run Code Online (Sandbox Code Playgroud)
实际上,您也可以将您的函数声明f()为constexpr并直接在其中使用enable_if,即std::enable_if<f<It>(), ...>.
| 归档时间: |
|
| 查看次数: |
930 次 |
| 最近记录: |