3
votes

Please consider this first snippet of code, in which a basic SFINAE trigger is used to discriminate whether a type is a random access iterator:

template <typename T, typename = void>
struct is_random_access_iterator : public std::false_type {};

template <typename T>
struct is_random_access_iterator<T, 
    std::enable_if_t<
        std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>
    >>
    : public std::true_type {};

template <typename T>
constexpr bool is_random_access_iterator_v = is_random_access_iterator<T>::value;

This code compiles and works just as expected. Now, consider this second snippet, where I substituted a template variable to the enable_if condition, without changing its definition at all:

template <typename T>
constexpr bool has_random_access_iterator_tag = 
    std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>;

template <typename T, typename = void>
struct is_random_access_iterator : public std::false_type {};

template <typename T>
struct is_random_access_iterator<T, 
    std::enable_if_t<
        //std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>
        has_random_access_iterator_tag<T>
    >>
    : public std::true_type {};

template <typename T>
constexpr bool is_random_access_iterator_v = is_random_access_iterator<T>::value;

SFINAE doesn't work anymore and the compiler (tested with gcc 8 and clang 7) complains about std::iterator_traits not existing whenever I supply a type it isn't specialized for.

Here's a working example:

#include <iostream>
#include <vector>
#include <iterator>
#include <type_traits>

template <typename T>
constexpr bool has_random_access_iterator_tag = 
    std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>;

template <typename T, typename = void>
struct is_random_access_iterator : public std::false_type {};

template <typename T>
struct is_random_access_iterator<T, 
    std::enable_if_t<
        //std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>
        has_random_access_iterator_tag<T>
    >>
    : public std::true_type {};

template <typename T>
constexpr bool is_random_access_iterator_v = is_random_access_iterator<T>::value;

int main() {
    std::cout   << std::boolalpha << "Is random access iterator:\n"
                << "- int:   " << is_random_access_iterator_v<int> << '\n'
                << "- int*:  " << is_random_access_iterator_v<int*> << '\n'
                << "- v::it: " << is_random_access_iterator_v<std::vector<int>::iterator> << '\n';
}

And the output:

prog.cc:8:54: error: no type named 'iterator_category' in 'std::__1::iterator_traits' std::is_same_v::iterator_category, std::random_access_iterator_tag>; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

prog.cc:17:9: note: in instantiation of variable template specialization 'has_random_access_iterator_tag' requested here has_random_access_iterator_tag ^

prog.cc:22:46: note: during template argument deduction for class template partial specialization 'is_random_access_iterator > >' [with T = int] constexpr bool is_random_access_iterator_v = is_random_access_iterator::value; ^

prog.cc:22:46: note: in instantiation of template class 'is_random_access_iterator' requested here prog.cc:26:35: note: in instantiation of variable template specialization 'is_random_access_iterator_v' requested here << "- int: " << is_random_access_iterator_v << '\n' ^ 1 error generated.

Can anyone explain me why? I'm frustrated by this because it feels very natural to use a template constant to make template programming more compact and readable.

1

1 Answers

8
votes

The issue here is that

template <typename T>
constexpr bool has_random_access_iterator_tag = 
    std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>;

Moves

std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>

out of a SFINAE context and puts it into just compiled code. SFINAE kicks when when substituting the deduced types for a template parameter fails durring overload resolution. Since

template <typename T>
constexpr bool has_random_access_iterator_tag = 
    std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>;

only has T as a template parameter there is no failure when you try and instantiate a has_random_access_iterator_tag<T>. Since there is no failure then

std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>;

gets compiled and if T is not valid for that expression then you get an error.

If you move the expression back into the template parameter then when the compiler deduces T it will use it to deduce

std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>;

and if that fails then it wont be an error since it happened durring the deduction of the template parameters. So if we have

template <typename T, bool Val = std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>>
constexpr bool has_random_access_iterator_tag = Val;

then if std::iterator_traits<T>::iterator_category is invalid the whole template is considered invalid and ignored.