Question

I am trying to write a method to transfer a unique_ptr from one std::vector to another.

template<typename T>
void transferOne(vector<std::unique_ptr<T> > &to, 
                 vector<std::unique_ptr<T> >::iterator what, 
                 vector<std::unique_ptr<T> > &from) {
    to.push_back(std::move(*what));
    from.erase(what);
}

Clang gives me an error: missing 'typename' prior to dependent type name 'vector >::iterator'

Any ideas how to deal with it?

Was it helpful?

Solution

As Clang tells you, put typename in front of the iterator type:

template<typename T>
void transferOne(vector<std::unique_ptr<T> > &to, 
                 typename vector<std::unique_ptr<T> >::iterator what, 
                 vector<std::unique_ptr<T> > &from) {
    to.push_back(std::move(*what));
    from.erase(what);
}

The keyword typename is used to tell the compiler, that vector<std::unique_ptr<T> >::iterator is a type. Without instantiating the template, the compiler can't in general find that out by itself, because there might be a specialization of the template vector, where the member iterator is a static variable instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top