Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 774
Hakaishin Avatar asked Jan 09 '17 16:01

Hakaishin


People also ask

What does STD Enable_if do?

enable_if is a meta-function that can help you conditionally remove functions from the overload resolution performed on the type-traits supplied to the function.

What is Enable_if in C++?

The enable_if family of templates is a set of tools to allow a function template or a class template specialization to include or exclude itself from a set of matching functions or specializations based on properties of its template arguments.


1 Answers

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, i.e. std::enable_if<f<It>(), ...>.

like image 143
Walter Avatar answered Oct 04 '22 07:10

Walter