P
P
Pinkman2021-10-24 01:42:05
C++ / C#
Pinkman, 2021-10-24 01:42:05

What does typename mean?

Good evening! There is a task to make an iterator for the stack (yes, I am aware that there are other containers =) ) I spied on the guys how to do this, but I can’t understand what it means:

typedef typename std::stack<T>::container_type::iterator iterator;


typedef I know.
typename - I'm not sure I understand this keyword. Outside , as I understand it, it is used to help the compiler in determining the type. what happens next ? does the stack have an iterator? template<typename T>
std::stack<T>::container_type::iterator

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2021-10-24
@famousman204

1. typenamein this case, the compiler is needed only as a hint from the developer that the subsequent identifier (ie std::stack<T>::container_type::iterator) is really a type name. The hint is needed because this typedef is probably also in the template, and we do not yet know what exactly the std::stack template is instantiated into (in this case, we say that container_type "is dependent on a template-parameter" - until we instantiate std ::stack, we won't know).
2. Member-type container_typeis equivalent to the type of the underlying container (because std::stack is an adapter for the stack interface, and not a real container, you choose the real storage container as the second template parameter, by default it is std::deque).
3. Here the std::deque<T>iterator really is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question