简体   繁体   English

如何正确使用enable_if?

[英]How to use enable_if correctly?

I need to learn how to use enable_if. 我需要学习如何使用enable_if。 For this I need to reimplement the distance function using enable_if. 为此,我需要使用enable_if重新实现distance函数。 I tried this: 我试过这个:

#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;
}

I thought I could do this by using some function like std::is_random_acces_iterator<In>::value similar to std::is_pod<In>::value I checked the type_traits but could not find anything for checking if something is a specific iterator. 我想我可以通过使用像std::is_random_acces_iterator<In>::value类似于std::is_pod<In>::value函数来做到这一点我检查了type_traits但是找不到任何东西来检查某些东西是否是特定的迭代器。 How would I check if something is an random_acces_iterator? 我如何检查是否有random_acces_iterator? I know I could just do this in a function: 我知道我可以在一个函数中执行此操作:

template<class T>
bool f(){
  typedef typename std::iterator_traits<T>::iterator_category cat;
  return std::random_access_iterator_tag == cat();
}

So my question is basically how do I do function f in a template? 所以我的问题基本上是如何在模板中执行函数f? And no not using enable_if is not possible, it is a requirement of my task. 不能不使用enable_if是不可能的,这是我的任务的要求。 And am I correct to believe that SFINAE would correctly discard the other function, if I could get function f into that template? 我是否正确相信SFINAE会正确地丢弃其他函数,如果我可以将函数f放入该模板中?

You can use the type traits std::is_same<> as follows 您可以使用类型特征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>;

and then 然后

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) { /* ... */ }

Here, std::enable_if_t<,> is a helper alias defined (since C++14) as 这里, std::enable_if_t<,>是一个定义的辅助别名(自C ++ 14起)

template<bool condition, typename type = void>
using enable_if_t = typename enable_if<condition,type>::type;

Actually, you can also declare your function f() as constexpr and use it directly within the enable_if , ie std::enable_if<f<It>(), ...> . 实际上,您也可以将函数f()声明为constexpr并直接在enable_if使用它,即std::enable_if<f<It>(), ...>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM