How to use enable_if correctly?

I need to learn to use enable_if. To do this, I need to override 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 using some type function std::is_random_acces_iterator<In>::valuesimilar to std::is_pod<In>::value I checked type_traits , but could not find anything to check if something is a specific iterator. How can I check if something is random_acces_iterator? I know that I can 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 can I use the f function in a template? And it’s impossible not to use enable_if, this is the requirement of my task. And do I believe that SFINAE will correctly drop another function if I could get the function f in this template?

+4
1

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

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

std::enable_if_t<,> , ( ++ 14)

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

, f() constexpr enable_if, .. std::enable_if<f<It>(), ...>.

+5

Source: https://habr.com/ru/post/1666282/


All Articles