请考虑第一段代码,其中使用基本SFINAE触发器来区分类型是否是随机访问迭代器:
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无效,则整个模板被视为无效并被忽略.
标签:c,templates,sfinae 来源: https://codeday.me/bug/20190731/1587504.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。