Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of C++ typedef typename

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the role of C++ typedef typename", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the role of C++ typedef typename?"

The typedef typename role of C++

Some of the grammars of C++ are puzzling, including:

Typedef typename std::vector::size_type size_type

one

For details, see C++ Primer (Fifth Edition) P584

Some do not understand the grammar sometimes although know what it probably means, ignore it, in fact, but the heart is always diaphragm, let's get to the bottom of it.

Vector::size_type

To understand the above grammar, you must first see the meaning of vector::size_type. Referring to "STL Source Code Analysis", it is not difficult to find, in fact:

Template class vector {public: / /... typedef size_t size_type; / /...}

one

two

three

four

five

six

seven

This makes it clear that vector::size_type is the nested type definition of vector, which is actually equivalent to the size_t type.

In other words:

Vector::size_type ssize;// is equivalent to size_t ssize.

one

two

three

Why use the typename keyword

So the question is, why add the typename keyword?

Typedef std::vector::size_type size_type;//why not?

one

In fact, the compiler doesn't know what a vector::size_type is until the template type is instantiated. In fact, there are three possibilities:

Static data member

Static member function

Nesting type

Then the role of typename is reflected at this time-the definition is no longer ambiguous.

Summary

So according to the above two analyses

Typedef typename std::vector::size_type size_type

one

The true face of the statement is:

Typedef creates aliases for existing types, while typename tells the compiler that std::vector::size_type is a type, not a member.

At this point, I believe you have a deeper understanding of "what is the role of C++ typedef typename". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report