In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use templates in C++ to improve the abstract level of code", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "how to use templates in C++ to improve the abstract level of code"!
T.1: Using Templates to Increase the Abstraction Level of Code
Reason
Generality. Reuse. Efficiency. Encourages consistent definition of user types.
universality. reuse. Efficiency. Encourage consistency in user types.
Example, bad
Conceptually, the following requirements are wrong because what we want of T is more than just the very low-level concepts of "can be incremented" or "can be added":
Conceptually, we want T not to be limited to very low-level concepts that can be incremented or added as a summand, so the following requirement is false.
template
// requires Incrementable
T sum1(vector& v, T s)
{
for (auto x : v) s += x;
return s;
}
template
// requires Simple_number
T sum2(vector& v, T s)
{
for (auto x : v) s = s + x;
return s;
}
Assuming that Incrementable does not support+ and Simple_number does not support +=, we have overconstrained implementers of sum1 and sum2. And, in this case, missed an opportunity for a generalization.
Assuming Incrementable does not support + and Simple_number does not support +=, we over-constrain implementers of sum1 and sum2. And, in this case, the opportunity for generalization is lost.
Example
template
// requires Arithmetic
T sum(vector& v, T s)
{
for (auto x : v) s += x;
return s;
}
Assuming that Arithmetic requires both + and +=, we have constrained the user of sum to provide a complete arithmetic type. That is not a minimal requirement, but it gives the implementer of algorithms much needed freedom and ensures that any Arithmetic type can be used for a wide variety of algorithms.
Assuming that arithmetic operations require both + and +=, we have asked users of sum to supply the full arithmetic type. This is not a minimal requirement, but it provides the implementer of the algorithm with the freedom needed and ensures that arithmetic types can be used for a wide variety of algorithms.
For additional generality and reusability, we could also use a more general Container or Range concept instead of committing to only one container, vector.
For extra versatility and reusability, we can also use more generic container or scope concepts instead of specific container vectors.
Note:
If we define a template to require exactly the operations required for a single implementation of a single algorithm (e.g., requiring just += rather than also = and +) and only those, we have overconstrained maintainers. We aim to minimize requirements on template arguments, but the absolutely minimal requirements of an implementation is rarely a meaningful concept.
If we define a template that requires operations for a particular implementation of a particular algorithm (e.g. only += but not both = and +) and only requires these, we are over-constraining the maintainer. Our goal is to minimize the requirements for template parameters, but absolute minimum requirements for an implementation hardly make sense.
Note:
Templates can be used to express essentially everything (they are Turing complete), but the aim of generic programming (as expressed using templates) is to efficiently generalize operations/algorithms over a set of types with similar semantic properties.
Templates can be used to express essentially anything (they are Turing-complete), but the purpose of generic programming (as expressed using templates) is to efficiently generalize to a set of types of operations/algorithms with similar semantic properties.
Note:
The requires in the comments are uses of concepts. "Concepts" are defined in an ISO Technical Specification: concepts. Concepts are supported in GCC 6.1 and later. Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only. If you use GCC 6.1 or later, you can uncomment them.
The requirement in the comment is the usage of concept. "Concepts" are defined in ISO specifications. All versions of GCC 6.1 and later support Concepts. Therefore, we commented out the relevant code in the example; that is, we used it as standard comments only. If you are using a later version of GCC 6.1, you can remove the comment symbol.
Enforcement
Flag algorithms with "overly simple" requirements, such as direct use of specific operators without a concept.
Flags require overly simple algorithms, such as using specific operators without concept.
Do not flag the definition of the "overly simple" concepts themselves; they may simply be building blocks for more useful concepts.
Don't mark concepts that are too simplistic in their definition; they may exist only as part of a more useful concept.
At this point, I believe that everyone has a deeper understanding of "how to use templates to improve the abstract level of code in C++," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to 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: 220
*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.