How can I create a container iterator with the container holding a policy type?

294 Views Asked by At

I have the following code:

#include <map>
#include <string>

class policy1
{
  public:
    struct data
    {
    };
};

template<typename policy>
class policy_user : public policy
{
  typedef std::map<std::string, typename policy::data> mymap;        // good 
  typedef std::map<std::string, 
           typename policy::data >::iterator myiterator;              // bad
  typedef mymap::iterator myseconditerator;                      // also bad
};

that fails with:

der.cpp:17: error: type ‘std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, typename policy::data, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, typename policy::data> > >’ is not derived from type ‘policy_user<policy>’
der.cpp:17: error: expected ‘;’ before ‘myiterator’
der.cpp:18: error: type ‘std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, typename policy::data, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, typename policy::data> > >’ is not derived from type ‘policy_user<policy>’
der.cpp:18: error: expected ‘;’ before ‘myseconditerator’

And I have no clue why. Any help how I can accomplish what I try to do (create an iterator)?

1

There are 1 best solutions below

0
On BEST ANSWER

You forgot typename in typedefs.

Write:

typedef typename std::map<std::string, typename policy::data >::iterator myiterator;      
typedef typename mymap::iterator myseconditerator; 

Now. It's fine. typename is required because iterator is a dependent name. See also this