Which type has this iterator?


1
2
3
4
5
6
7
auto itr = mResourceMap.find( id ); // Which type has this iterator?

// I thought it would be of this, but it isn't.
std::map<Identifier,std::unique_ptr<Resource>>
                     ::iterator itr = mResourceMap.find( id );

// mResourceMap is of type: std::map<Identifier,std::unique_ptr<Resource>> 

std::map<Identifier,std::unique_ptr<Resource>>::const_iterator if find is called on a const map;

std::map<Identifier,std::unique_ptr<Resource>>::iterator otherwise.
iHere the whole function:
1
2
3
4
5
6
7
8
9
template <typename Resource, typename Identifier>
const Resource & ResourceHolder<Resource, Identifier>
::get( Identifier id) const
{
    std::map<Identifier,std::unique_ptr<Resource>>
    ::iterator itr = mResourceMap.find( id );
    assert (itr != mResourceMap.end() );
    return *itr->second;
}

And there I get this error(s):
1
2
3
4
5
6
7
8
9
10
11
12
13
ResourceHolder.hpp: In member function ‘const Resource& ResourceHolder<Resource, Identifier>::get(Identifier) const’:
ResourceHolder.hpp:53:5: error: need ‘typename’ before ‘std::map<Identifier, std::unique_ptr<_Tp> >::iterator’ 
because ‘std::map<Identifier, std::unique_ptr<_Tp> >’ is a dependent scope
     std::map<Identifier,std::unique_ptr<Resource>>::iterator itr = mResourceMap.find( id );
     ^~~
ResourceHolder.hpp:53:62: error: expected ‘;’ before ‘itr’
     std::map<Identifier,std::unique_ptr<Resource>>::iterator itr = mResourceMap.find( id );
                                                              ^~~
In file included from /usr/include/c++/7/cassert:44:0,
                 from ResourceHolder.hpp:30,
                 from test.cpp:5:
ResourceHolder.hpp:54:13: error: ‘itr’ was not declared in this scope
     assert (itr != mResourceMap.end() );

Last edited on
In member function ‘const Resource& ResourceHolder<Resource, Identifier>::get(Identifier) const’:

As JLBorges said, const_iterator.
1
2
3
4
5
6
7
template < typename Resource, typename Identifier >
const Resource & ResourceHolder<Resource, Identifier>::get( Identifier id) const
{
    typename std::map<Identifier,std::unique_ptr<Resource>>::const_iterator itr = mResourceMap.find( id );

    // ...
}
Thanks, it works at my example with both ::iterator and ::const_iterator.

Any reasons why 'typename' is required? That's not what I would expect.
> Any reasons why 'typename' is required? That's not what I would expect.

See: http://www.cplusplus.com/forum/general/219039/#msg1009625
Thx, your link was very insightful to the topic.
Topic archived. No new replies allowed.