1

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. 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. But things have changed and I cannot use such features anymore.

I am trying the achieve same functionally without the keyword 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.

ssube
  • 47,010
  • 7
  • 103
  • 140
KiaMorot
  • 1,668
  • 11
  • 22

1 Answers1

6

You should use typename Type::iterator since it's dependent-name. Read more here

Community
  • 1
  • 1
ForEveR
  • 55,233
  • 2
  • 119
  • 133