简体   繁体   English

在函数模板中推导迭代器类型?

[英]Deduce iterator type in function template?

I'm trying to deduce the type of an iterator in a function which is already deducing the argument's type with a template. 我试图在已经通过模板推断出参数类型的函数中推断出迭代器的类型。 What I am trying to achieve is substitution for the keyword auto which has similar capability in C++11 standard. 我想要实现的是替换关键字auto ,它在C ++ 11标准中具有类似的功能。 Originally I had the following function: 最初,我具有以下功能:

    template<typename Type> bool activeExtension(Type &arr)
    {
        for (auto it=arr.begin(),
                  ite=arr.end();
                  it!=ite;
                      ++it)
        {
            if (*it != 0)
               return true;
        }
        return false;
    }

This works perfectly compiling for C++11 standard. 这可以完美地为C ++ 11标准编译。 But things have changed and I cannot use such features anymore. 但是情况已经改变,我不能再使用这些功能。

I am trying the achieve same functionally without the keyword auto . 我正在尝试在没有关键字auto的情况下实现相同的功能。 So I thought about the templates. 所以我想到了模板。

So far what I tried is this: 到目前为止,我尝试过的是:

    template<typename Type> bool activeExtension(Type &arr)
    {
        for (Type::iterator  it=arr.begin(),
                             ite=arr.end();
                             it!=ite;
                         ++it)
        {
            if (*it != 0)
                return true;
        }
        return false;
    }

How would you go to solve this? 您将如何解决这个问题?

Note: I usually call this function with the following type, 注意:我通常使用以下类型调用此函数,

template <class T>
struct Generic_t {
    typedef std::vector<T> Array;
};

as I have to instantiate a vector with different types. 因为我必须实例化具有不同类型的向量。

You should use typename Type::iterator since it's dependent-name. 您应该使用typename Type::iterator因为它是从属名称。 Read more here 在这里阅读更多

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

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