문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top