Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type and Dependent Name

Manually I can create a std::vector<int>::iterator object like:

std::vector<int>::iterator i;

So here std::vector<int>::iterator is a type. But when I write a function :

template <class T>
std::vector<T>::iterator foo(std::vector<int>::iterator i)
{
    return i;
}

The compiler shows a warning :

std::vector<T>::iterator' : is a dependent name  not a type

and the code does not compiles. But if in main I call the function like this:

int main()
{
    vector<int> v;
    foo(v.begin());
}

The parameter T should be resolved. Then why compiler is showing error?

like image 251
deeiip Avatar asked Dec 04 '13 06:12

deeiip


2 Answers

You have to prefix it with Typename std::vector<T>::iterator is dependent on a template parameter, namely T.Therefore,you should prefix with it typename: Try using:

typename std::vector<T>::iterator

You can refer This :http://pages.cs.wisc.edu/~driscoll/typename.html

like image 156
Biraj Borah Avatar answered Sep 28 '22 10:09

Biraj Borah


Following link has a solution and answer,

"Dependent Name is not a Type", but prefixing with "typename" causes compiler crash

In short, "std::vector::iterator" is a dependent name not a typename. So you can not use directly as a typename. You have to specify "typename std::vector::iterator".

like image 35
Saravanan Avatar answered Sep 28 '22 09:09

Saravanan