In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how C++ uses templates to express containers and ranges". In daily operation, I believe many people have doubts about how C++ uses templates to express containers and ranges. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "how C++ uses templates to express containers and ranges". Next, please follow the editor to study!
T.3: use templates to represent containers and ranges
Reason (reason)
Containers need an element type, and expressing that as a template argument is general, reusable, and type safe. It also avoids brittle or inefficient workarounds. Convention: That's the way the STL does it.
The container needs to know the element type, and it is common, reusable, and type safe to represent the element type as a template parameter. It can avoid vulnerability and inefficient flexibility. As a rule, that's what STL does.
Example (sample)
Template
/ / requires Regular
Class Vector {
/ /...
T * elem; / / points to sz Ts
Int sz
}
Vector v (10)
V [7] = 9.9 × example, bad (negative example) class Container {
/ /...
Void* elem; / / points to size elements of some type
Int sz
}
Container c (10, sizeof (double))
(double*) c.elem) [7] = 9.9
This doesn't directly express the intent of the programmer and hides the structure of the program from the type system and optimizer.
Hiding the void* behind macros simply obscures the problems and introduces new opportunities for confusion.
This code does not directly express the programmer's intention and hides the program structure from the type system and optimizer. Masking void* with macro definitions will only blur the problem and further increase the chances of confusion.
Exceptions: If you need an ABI-stable interface, you might have to provide a base implementation and express the (type-safe) template in terms of that. See Stable base.
Exception: if you need a stable interface for ABI, you may have to provide a basic implementation and represent the template according to its concept.
Enforcement (implementation recommendations)
Flag uses of void*s and casts outside low-level implementation code
Marks cases where void* is used and low-level type conversions are used in the external implementation code.
At this point, the study on "how C++ uses templates to represent containers and ranges" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.