sfinae 上下文中的模板变量 [英] Template variable in sfinae context

查看:45
本文介绍了sfinae 上下文中的模板变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑第一段代码,其中使用基本的 SFINAE 触发器来区分类型是否为随机访问迭代器:

template 结构 is_random_access_iterator : 公共 std::false_type {};模板 struct is_random_access_iterator::iterator_category, std::random_access_iterator_tag>>>:公共 std::true_type {};模板 constexpr bool is_random_access_iterator_v = is_random_access_iterator::value;

此代码编译并按预期工作.现在,考虑第二个片段,其中我将模板变量替换为 enable_if 条件,而根本没有更改其定义:

template constexpr bool has_random_access_iterator_tag =std::is_same_v::iterator_category, std::random_access_iterator_tag>;模板 结构 is_random_access_iterator : 公共 std::false_type {};模板 struct is_random_access_iterator::iterator_category, std::random_access_iterator_tag>has_random_access_iterator_tag<T>>>:公共 std::true_type {};模板 constexpr bool is_random_access_iterator_v = is_random_access_iterator::value;

SFINAE 不再工作,编译器(用 gcc 8 和 clang 7 测试)抱怨 std::iterator_traits 不存在,只要我提供它不是专门用于的类型.>

这是一个工作示例:

#include #include <向量>#include <迭代器>#include 模板 constexpr bool has_random_access_iterator_tag =std::is_same_v::iterator_category, std::random_access_iterator_tag>;模板 结构 is_random_access_iterator : 公共 std::false_type {};模板 struct is_random_access_iterator::iterator_category, std::random_access_iterator_tag>has_random_access_iterator_tag<T>>>:公共 std::true_type {};模板 constexpr bool is_random_access_iterator_v = is_random_access_iterator::value;int main() {std::cout <<std::boolalpha <<"是随机访问迭代器:\n"<<"- int:" <<is_random_access_iterator_v<<'\n'<<"- int*: " <<is_random_access_iterator_v<<'\n'<<"- v::it:" <<is_random_access_iterator_v::iterator><<'\n';}

和输出:

<块引用>

prog.cc:8:54: 错误:在std::__1::iterator_traits"中没有名为iterator_category"的类型std::is_same_v::iterator_category, std::random_access_iterator_tag>;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

prog.cc:17:9:注意:在此处请求的变量模板特化has_random_access_iterator_tag"的实例化has_random_access_iterator_tag^

prog.cc:22:46:注意:在类模板部分特化的模板参数推导过程中 'is_random_access_iterator > >' [with T = int]constexpr bool is_random_access_iterator_v = is_random_access_iterator::value;^

prog.cc:22:46:注意:在此处请求的模板类is_random_access_iterator"的实例化中prog.cc:26:35:注意:在此处请求的变量模板特化is_random_access_iterator_v"的实例化<<"- int:" <<is_random_access_iterator_v <<'\n'^产生了 1 个错误.

谁能解释一下为什么?我对此感到沮丧,因为使用模板常量使模板编程更紧凑和可读性感觉很自然.

解决方案

这里的问题是

template constexpr bool has_random_access_iterator_tag =std::is_same_v::iterator_category, std::random_access_iterator_tag>;

移动

std::is_same_v::iterator_category, std::random_access_iterator_tag>

脱离 SFINAE 上下文并将其放入刚刚编译的代码中.当在重载解析期间用推导类型替换模板参数失败时,SFINAE 会启动.由于

template constexpr bool has_random_access_iterator_tag =std::is_same_v::iterator_category, std::random_access_iterator_tag>;

只有 T 作为模板参数,当您尝试实例化 has_random_access_iterator_tag 时不会失败.既然没有失败那么

std::is_same_v::iterator_category, std::random_access_iterator_tag>;

被编译,如果 T 对该表达式无效,那么你会得到一个错误.

如果您将表达式移回模板参数中,那么当编译器推导 T 时,它将使用它来推导

std::is_same_v::iterator_category, std::random_access_iterator_tag>;

如果失败,那么它不会是一个错误,因为它发生在模板参数的推导过程中.所以如果我们有

template ::iterator_category, std::random_access_iterator_tag>>constexpr bool has_random_access_iterator_tag = Val;

那么如果 std::iterator_traits<T>::iterator_category 无效,则整个模板被认为无效并被忽略.

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.

解决方案

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.

这篇关于sfinae 上下文中的模板变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆