0

I have this code:

template <typename T>
class CircularBuffer
{
 public:
  typename typedef std::list<T>::iterator iterator;

  //other code
};

What does the typename in the iterator declaration do? I know the typedef is saying "I have a list iterator that i want to call 'iterator'. But what is the purpose of the typename? Is it just cause my list can store any typename T that I need it? Is it for something else?

Thanks,

Tom

Edit: Is it because the list iterator is a qualified dependent type, and so I need typename before it? Or, more correctly, the typedef I am defining, 'iterator' which is based on the type std::list::iterator is a qualified dependent type since it is an iterator of a qualified dependent list?

traggatmot
  • 1,423
  • 5
  • 26
  • 51
  • Shot answer: because the compiler can't figure out if `X::Y` is a static class variable or a type name without expanding the template, so it assumes that it's a static class variable unless you explicitly say that it's a type name with the `typename` keyword. – Adam Rosenfield Mar 07 '13 at 02:28
  • It's `typedef typename`, not vice versa. Is that a copy-paste error? – Potatoswatter Mar 07 '13 at 02:33
  • That is not a copy-paste error, that is from an old test that I am studying - it is actually in the Professor's solution code for the overall problem. I can't imagine he has it wrong..... – traggatmot Mar 07 '13 at 02:36
  • 1
    @traggatmot Imagine it. Definitely wrong, and meaningless. – Potatoswatter Mar 07 '13 at 02:37
  • @Potatoswatter, so just reversing it makes it correct? OK. And then typename is used to indicate that T is a typename, right? – traggatmot Mar 07 '13 at 02:45
  • 1
    @traggatmot Yes. Then it indicates that `std::list::iterator` is a typename. It associates with the whole series of items joined by `::` but applies only to the last, namely `iterator`. It would probably be clearer if it were written `std::list::typename iterator`, and the `template` keyword does work that way (see other Q&A), but I think the `typename` rule was specced first so we're stuck with it. – Potatoswatter Mar 07 '13 at 02:49

0 Answers0