Author: Tang Feng
Home: http://www.cnblogs.com/liyiwen
Rvalue reference, lambda, and variadic template are the most important improvements to the C ++ 11 language core. Rvalue rules are a little complicated, but once you understand and remember them, there is no difficulty in application. Lambda is actually a "natural" language facility. In addition to a slightly strange syntax, Lambda can be used immediately and widely used. The new feature of variadic template is not similar to the previous two. Its syntax rules are not complex, but it is really tricky for applications. Fortunately, this technology is mainly used in the implementation of libraries, A well-implemented library does not allow us to make any requirements on the implementation details.
The variadic template feature is a natural requirement. It improves the template design method of C ++. The original template parameters can make the parameter types of classes and functions "arbitrary". If you add "arbitrary number of parameters ", then the design methods for parameters are basically complete. With variadic template, we can obviously make the designed functions or classes more reusable. Because a lot of processing operations have little to do with the number of processing objects, such as screen playing (printf), for example, comparing the size (max, min), such as function binding sub (bind, the number and type of parameters required for a function to correspond to various possible functions ). If no parameter can be matched, there will always be people who cannot reuse the existing implementation, and they have to repeat the processing they need, the implementers of the common library have to repeatedly write a lot of code or use strange techniques to make their classes (functions) reusable in more scenarios as much as possible, macro and so on. (Such as bind in tr1 ).
I. Basic syntax
Syntax:
- Template <typename element> class tuple;
-
- Tuple <int, string> A; // use it like this
The ellipsis (...) appears on the left of the template parameter element, indicating that the element is a template type parameter pack ). Parameter pack (parameter package) is a new concept introduced in C ++. For example, in this example, element indicates that it is a package consisting of a series of arbitrary parameters. For example, in row 2nd, element is a collection of int and string parameters. You can do this not only for "type" template parameters (that is, the parameters defined by typename), but also for non-type template parameters. For example:
- Template <typename T, unsigned primarydimesion, unsigned... dimesions>
- Class Array {/**/};
-
- Array <double, 3, 3> rotation_matrix; // 3x3 ratiation Matrix
Now that we know the parameter pack, how can we actually process the packaged "any number" parameters in the program? I originally thought that the compiler would provide some built-in "parameter extraction functions" such as get_param <1> (element) for programmers to use !! It seems that my thinking is too "procedural ". In fact, C ++ 11 uses unpack and "template-specific" like function overload to extract parameters. This is the most "pitfall" part of the application of variadic tempate, because it requires some skill in "recursion" and "human code expansion. Let's look at the example:
- Template <typename... elements> class tuple;
- Template <typename head, typename... tail>
- Class tuple
- Head head;
- Public:
- /* Implementation */
- };
- Template <>
- Class tuple <> {
- /* Zero-tuple implementation */
- };
Rows 1st declare a tuple class that can correspond to any parameter. Rows 2nd to 7 declare a specific part of this class. Note that this is a typical method for extracting parameters. It is easier to give a graph to understand:
(If not all, click it to see the full picture)
Well, I think this figure is the most valuable part of this blog. You must specify the source for reprinting!
It only describes another concept of parameter pack. The template parameters are followed by ellipsis... is an unpack. The list of parameters represented by this parameter is unpacked, and a new template is matched or expanded. Others, I think there is no need to pay more attention to the above figure.
Ii. Example
Many of the new standard libraries depend on the language features of variadic template, such as tuple, bind, and function. But they are complicated and not "amazing ". C ++'s fqa and Wikipedia examples of C ++ 11 are "type-safe" printf:
- Void printf (const char * s)
- {
- While (* s ){
- If (* s = '% '){
- If (* (S + 1) = '% '){
- ++ S;
- }
- Else {
- Throw STD: runtime_error ("invalid format string: Missing arguments ");
- }
- }
- STD: cout <* s ++;
- }
- }
-
- Template <typename T, typename... ARGs>
- Void printf (const char * s, t value, argS... ARGs)
- {
- While (* s ){
- If (* s = '% '){
- If (* (S + 1) = '% '){
- ++ S;
- }
- Else {
- STD: cout <value;
- // Call even when * s = 0 to detect extra arguments
- Printf (S + 1, argS ...);
- Return;
- }
- }
- STD: cout <* s ++;
- }
- Throw STD: logic_error ("extra arguments provided to printf ");
- }
This example is really good and short enough. In the code of Line X, select (IF), loop (while), recursion (recursion of printf itself ), overload (cout <overload of Type output type), exception. This example is not detailed. Let's try to analyze it with the "above figure. (This example is used for interviews. Is it good to ask the interviewer to explain it ?......)
3. Miscellaneous
Here is an article about the motivation for adding this feature to C ++. In short, functions and bind will be very troublesome to implement without this feature, the Code implemented in this way also makes the compilation time very long.
You can see that if 39 parameters are supported, the Compilation Time will reach 700 seconds. After the variadic template is used, the code is reduced by 40 kb, and the compilation time is less than 1 second in case of the number of parameters.
This is good. However, this feature is really not that easy to use, as you can see. When used together with other features (multiple inheritance, right value reference), it is more "complex ". However, it is a powerful tool that can create a very useful library. Otherwise, we will use the library. Don't worry about learning, remember, or use it in daily programming.
Iv. References
- Variadic templates for GCC: http://www.generic-programming.org /~ Dgregor, CPP, variadic-templates.html
- Wikipedia-http://en.wikipedia.org/wiki/Variadic_template
- C ++ 11 proposal at that time: n2080 (this is particularly good or best, what I have seen)