Home Template variable in sfinae context
Reply: 0

Template variable in sfinae context

user4477
1#
user4477 Published in April 22, 2018, 6:14 pm

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304421 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO