In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 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 how to use the variable parameter template of Clear11. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the Clear11 variable parameter template.
Variable parameter function
In C language, variable parameter function can be said to be a magical existence. For example, the most commonly used printf function is based on the following prototype:
Its first parameter is the format of type const char*, and the type and name of the subsequent parameters are not defined, only three points.
Although from the C language compilation point of view, at this location programmers can write any number of parameters of any type, in fact, the printf function according to the format string specified by format to use the later specified argument value, a small number or type errors often lead to errors. The number of variable parameters and the type should be passed through the previous parameters, which is the first difficulty of variable parameters.
Since the prototype of the variable parameter function only says that the parameters are variable, but does not specify where to store these variable parameters, another method is needed to generate the parameter list. For example, the following sample code:
First define the parameter list, then get the contents of the variable list through va_start, and then use va_end to release the variable list. This necessary step is the second difficulty in variable argument functions.
Variable parameter template declaration
Variable parameter templates have been added to Category 11, which can accept a variable number of parameters of a type. We introduce the use of variable parameter templates through examples of outputting debugging information commonly used in development. The first is to declare a variable parameter template.
In the part of the template parameter definition, through typename... Define variable template parameters. The meaning of the three points is similar to the definition of variable parameters in C, except that the name Args of the variable parameter list is then specified.
In the parameter definition section, the variable parameter list of the function is defined in the same format using the Args defined by the variable template parameter.
This parameter list can be used directly in the implementation of the template function, so that the second difficulty of the variable parameter function is solved when defining the variable parameter template.
Implementation of variable Parameter template
The implementation of variable parameter templates usually requires some tricks: recursion and overloading. Let's look at the code first.
Two overloaded writeLog functions are defined in the code, one accepts only the parameter t of type T, and the other accepts the variable parameter rest in addition to t. When writeLog is called with one parameter, the above function is actually called; when writeLog is called with multiple parameters, the following writeLog is called.
The following writeLog first calls the above writeLog with the first parameter t, and then recursively calls writeLog using rest (strictly speaking, when there is more than one parameter in the rest). From the caller's point of view, after processing one parameter at a time, the writeLog is called again with the remaining parameters until the writeLog of one parameter is finally called.
Here is the sample code that uses writeLog:
You can combine the type and number of parameters at will, and no additional information is required. In this way, the first difficulty of variable parameter function is solved effectively.
One step closer.
In this example, the writeLog of a parameter is very simple, simply using cout for output, and if there is a special need, you can continue to overload this function. For example, the time information that is often needed in the log output can be achieved as follows:
It is easier to use:
Output result
The font is as ugly as ever, but the content is magical enough!
At this point, I believe that you have a deeper understanding of "how to use the 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.