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;
此代码编译并按预期工作.现在,考虑第二个片段,我将模板变量替换为enable_if条件,而根本不更改其定义:
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不再起作用了,编译器(用gcc 8和clang 7测试)抱怨std :: iterator_traits不存在,只要我提供一个不专门的类型.
这是一个有效的例子:
#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'; }
并输出:
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.
谁能解释我为什么?我对此感到沮丧,因为使用模板常量使模板编程更加紧凑和易读是非常自然的.
这里的问题是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>;
移动
std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>
超出SFINAE上下文并将其放入已编译的代码中.当替换推导类型的模板参数时,SFINAE会在重载决策失败时启动.以来
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>;
只有T作为模板参数,当您尝试实例化has_random_access_iterator_tag< T>时,没有失败.既然没有失败那么
std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>;
得到编译,如果T对该表达式无效,则会出错.
如果将表达式移回模板参数,那么当编译器推导出T时,它将使用它来推导
std::is_same_v<typename std::iterator_traits<T>::iterator_category, std::random_access_iterator_tag>;
如果失败那么它就不会出错,因为它在模板参数的推导下发生了.所以,如果我们有
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;
然后,如果std :: iterator_traits< T> :: iterator_category无效,则整个模板被视为无效并被忽略.