Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with iterators for std::list of boost::shared_ptr

I'm having a problem with the following code:

#include <list>
#include <boost/shared_ptr.hpp>
#include "Protocol/IMessage.hpp"

template <typename HeaderType>
class Connection {
 public:
  typedef IMessage<HeaderType>             MessageType;
  typedef boost::shared_ptr<MessageType>   MessagePointer; 

  template <typename Handler>
  void
  FlushMessageQueue(Handler handler) {
    std::list<MessagePointer>::iterator ib = message_queue_.begin(); // line 69
    std::list<MessagePointer>::iterator ie = message_queue_.end();
    for (; ib != ie; ++ib) {
      AsyncWrite(*ib, handler);
    }
  }

 private:
  std::list<MessagePointer> message_queue_;
};

gcc (4.2.1) tells me:

include/Network/Connection.hpp: In member function 'void Network::Connection<MT>::FlushMessageQueue(Handler)':
include/Network/Connection.hpp:69: error: expected `;' before 'ib'

I wonder why I can't create an iterator for a list of MessagePointer's.

Any ideas?

Thank you.

like image 339
Bertrand Marron Avatar asked Dec 22 '22 07:12

Bertrand Marron


1 Answers

std::list<MessagePointer> in your code is a dependent type (i.e. it depends on the type of a template argument). Consequently, you need to use typename to state that ::iterator is expected to be a type for all potential instantiations (as it can be a value for some of them, if they are specialized). So:

typename std::list<MessagePointer>::iterator ib = message_queue_.begin();
like image 122
Pavel Minaev Avatar answered Dec 24 '22 21:12

Pavel Minaev