简体   繁体   中英

How to use enable_if correctly?

I need to learn how to use enable_if. For this I need to reimplement the distance function using enable_if. 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. How would I check if something is an 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? And no not using enable_if is not possible, it is a requirement of my task. And am I correct to believe that SFINAE would correctly discard the other function, if I could get function f into that template?

You can use the type traits std::is_same<> as follows

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

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>(), ...> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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