0

I am instantiating an iterator within a method in one of my templates. It works when I use auto to dynamically obtain type information:

auto itr = my_list.cbegin();

However, I get a compile-time error when I use the actual type:

typedef InternalEdge network_internal_edge_type;
typedef std::shared_ptr< network_internal_edge_type > network_shared_edge_ptr;
typedef std::list< network_shared_edge_ptr > network_internal_edge_store_type;

network_internal_edge_store_type::const_iterator itr = internal_edges_.cbegin();

InternalEdge is a private class I had defined somewhere above in the code. Here is the error:

Network.hpp:411:5: error: need ‘typename’ before ‘Network<V>::internal_edge_store_mechanism:: const_iterator’ because ‘Network<V>::internal_edge_store_mechanism’ is a dependent scope
Network.hpp:411:46: error: expected ‘;’ before ‘itr’
Network.hpp:412:12: error: ‘itr’ was not declared in this scope

Why does using auto make the code work? Why does the compiler think I need "typename"?

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206
  • Because it's dependent name in template-function. Read: http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords – ForEveR Feb 19 '13 at 11:06
  • Wow, that is actually a very useful compiler error. May I ask which compiler/version you are using? – juanchopanza Feb 19 '13 at 11:11
  • @juanchopanza: looks like a relatively recent GCC. –  Feb 19 '13 at 11:17
  • @Fanael Ah OK, I guess I haven't triggered that kind of error recently then. Thanks for the info. – juanchopanza Feb 19 '13 at 11:23

0 Answers0