简体   繁体   English

为泛型函数定义自定义迭代器特征

[英]Defining a custom iterator traits for a generic function

I'm trying to write a generic function which will derive a return type at compile time according to the iterator it is given. 我正在尝试编写一个泛型函数,该函数将根据给定的迭代器在编译时派生返回类型。 Usually this is done through std::iterator_traits, but I also wanted to define my own version of iterator_traits, called my_iterator traits. 通常,这是通过std :: iterator_traits完成的,但是我也想定义自己的iterator_traits版本,称为my_iterator traits。 Here's my code: 这是我的代码:

#include <cassert>
#include <iterator>
#include <vector>

using namespace std;

template <typename I>
struct my_iterator_traits {
    typedef typename I::value_type                     value_type;
    typedef typename I::iterator_category       iterator_category;
};

template <typename T>
struct my_iterator_traits<T*> {
    typedef T                                         value_type;
    typedef std::random_access_iterator_tag    iterator_category;
};                                                               

template <typename II>
typename my_iterator_traits<II>::value_type f(II b, II e) {
    typename my_iterator_traits<II>::value_type val = 0;
    return val;
}

int main () {
    int a[] = {2, 3, 4};
    int i = f(a, a + 3);

    assert(i == 0);

    // vector<int> v = {2};
    // f(v.begin(), v.end());

    // assert(j == 0);
    return 0;
}

Everything up to and including the main function and the function f() makes perfect sense. 包括main函数和函数f()在内的所有内容都是很合理的。 In main, I make an int array, call f() on it, and confirm that the output I get is an int whose value is zero. 在main中,我创建一个int数组,对其调用f() ,并确认我得到的输出是一个值为0的int。

What isn't clear is the following. 尚不清楚的是以下内容。 I have two templates up top followed by the struct keyword. 我有两个模板,其次是struct关键字。 It would make perfect sense to use the second one (the one that takes in T* as the template argument). 最好使用第二个参数(以T*作为模板参数的参数)。 Specifically, why do we need the first struct template (the one without template parameters)? 具体来说,为什么我们需要第一个结构模板(一个没有模板参数的结构模板)? More importantly, what is the relationship between the two struct templates? 更重要的是,两个结构模板之间是什么关系?

The first is the declaration of the template and the second is a partial specialization of the first. 一个是模板的声明, 第二个是第一个的部分专业化 Partial specialization like explained in the link can have customization of the template parameter (ex: some fixed parameters, some restriction of what parameter your are referring (pointer, references, etc...), etc...). 像链接中所述的部分专业化可以对模板参数进行自定义(例如:一些固定参数,对您要引用的参数(指针,引用等)的某些限制等)。

The first template<typename>struct is a template , the second is a specialization of the first template . 第一个template<typename>struct是一个template ,第二个是第一个template

template specialization is something similar to overriding (but not really) that is based off of pattern matching. template类似于基于模式匹配的覆盖(但不是真的)。 If the arguments to the template can be matched by the pattern of a specialization, it is used instead. 如果template的参数可以通过专业化模式进行匹配,则使用它代替。

This pattern matching does not do type conversion in the same way that function overrides do. 这种模式匹配不会像函数重写那样进行类型转换。 SFINAE is in play however, for advanced work. SFINAE正在为高级工作而努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM