In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
The main content of this article is to explain the "parameter forwarding example analysis of Clear11 variable parameter template". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "Clear11 variable parameter template of the parameter forwarding example analysis"!
Example
Many software systems have log function, through which the state and action of the system can be confirmed. Most of the logs are in text form. Although it is very convenient, due to the limitations of the text form itself, the loss of information will occur in the stored procedure.
This paper implements an information storage class that can store a variety of information.
Segment is an abstract class that defines the basic shape of a piece of information. The code is as follows:
Since it is only a prototype, only an output operation is defined and the output operator is overloaded. The purpose is to output the content of the information in text form.
Next are two derived classes that store integer information and string information, respectively.
The content of the IntSegment class is simple and is omitted here.
The StringSegment class needs to note that there are two versions of the constructor, corresponding to the left-value reference parameter and the right-value reference parameter. When the parameter type is a left-value reference, the m_msg member executes the copy constructor; when the parameter type is a right-value reference, the m_msg member executes the move constructor.
Finally, there is the MsgHolder class.
The MsgHolder class uses a vector to hold pieces of information and uses output in conjunction with the output operator to output information in text form.
MsgHolder also provides three additional add function templates, the first two fixed parameter add methods are used to create IntSegment and StringSegment objects, and variable parameter methods are used to accept an unspecified majority of parameters and forward them.
Variable parameter template parameter forwarding
Like fixed parameter template functions, variable parameter templates also have reference merging, so std::forward is also used in the process of passing template parameters. Its usage is as follows:
Add (std::forward (rest)...)
Example of MsgHolder usage
When the above preparations are done, you can use them. The sample code is as follows:
In the add method, integers, string constants, left-value references, and right-value references are input, respectively. The correct output is produced for each case; the move operation also occurs in the case of the right value reference.
In other words, when the user wants to continue using the string (in the case of ltest), the parameter is passed normally, and a copy of the string is stored; when the user clearly no longer needs a string (in the case of rtest), use std::move to notify the compiler, in which case the string itself is stored without copy action.
If std::forward is not used correctly, the part referenced by the right value will not work properly. In either case, a string copy occurs.
About extending the extension of the information storage class
The current MsgHolder is still in its infancy, and you can add derived classes of Segment to extend the supported data types, or you can add output methods to provide other forms of output.
At this point, I believe that you have a deeper understanding of the "parameter forwarding example analysis of Clear11 variable parameter template". 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.
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.