How to use typename in c++?

1.9k Views Asked by At

I have read the book named C++ primer and I don't understand the following code:

typedef typename std::vector<int>::size_type size_type;

Could you help me explain the use of typename here?

1

There are 1 best solutions below

0
On BEST ANSWER

You can read typedef typename std::vector::size_type size_type like this:

typedef typename std::vector::size_type size_type, just like typedef __int64 INT64.

Why we need typename beforce std::vector::size_type? It just tells the compiler that std::vector::size_type is a type not a normal class member. It's used for disambiguation.

But I think maybe some compiler can auto detect std::vector::size_type is a type.

So, typedef just creates an alias for an existing type, and typename tells the compile that std::vector::size_type is a type not a normal class member.