The typename keyword, is needed because iterator is a dependent type on P. The compiler can't guess if iterator refers to a value or a type, so it assume its a value unless you yell typename. It's needed whenever there is a type dependent on a template argument, in a context that either types or values would be valid. For instance, as base classes typename is not needed since a base class must be a type.
On the same subject, there is a template keyword used to let the compiler know that some dependent name is a template function instead of a value.