The tuple introduced in C ++ 11 is an N-tuple. It is equivalent to a struct with n members, but the members of this struct are anonymous. Tuple has two special functions, one is head (), used to obtain the value of the first Member, and the other is tail (), used to obtain the value of all the remaining members, tail () it is a tuple. In this way, if we want to take the value of the second member in tuple, We can first take the value of tail () and then the value of head () of tail. Of course, this is troublesome to use, so c ++ 11 provides the get function to obtain the value of a Member in tuple through indexes. In addition, make_tuple can be used to easily construct a tuple object. For examples of using tuple, refer to the followingCode.
1 Tuple < Int , Char , String > Tupinfo ( 10 , ' A ' , " Hello World " );
2 Int A = tupinfo. Head ();
3 Int A2 = tupinfo. Tail (). Head ();
4 Tuple < Char , String > Tuptail = tupinfo. Tail ();
5 Int B = Get < 0 > (Tupinfo );
6 Char C = Get < 1 > (Tupinfo );
7 String S = Get < 2 > (Tupinfo );
8 Auto tupinfo2 = make_tuple ( 5 , ' B ' , String ( " C ++ 11 " ), 4.6 );
As mentioned above, tuple is an N-tuples, and there is no limit on the number of N. That is to say, tuple can contain 0, 1, 2, or more elements, the type of each element is specified by template parameters. So how does tuple do this? The answer is a variable parameter template.
People who learn C ++ should be familiar with the printf function. One feature of printf is that the number of parameters is variable. In C ++ 11, the number of parameters allowed in the template is variable. The following is a variable template parameter function template, used to obtain the number of input parameters.
1 Template <typename... ARGs>
2 Uint getparametercount (ARGs... ARGs)
3 {
4 Return Sizeof ... (ARGs );
5 }
You can see that the variable parameter template uses typename plus... to represent the template parameter package, and ARGs plus... to represent the function parameter package. The sizeof... in the code above is used to obtain the number of parameters in the function parameter package. Its parameters must be an object of the function parameter package type. After getting familiar with the basic syntax of the Variable Parameter template, we will use it to compile a print function. The number and type of parameters of this function are variable, it simply outputs the values of each input parameter. The values are separated by commas and the line feed is automatically generated after the value of the last parameter is output.
1 Template <typename T>
2 Void Print (T value)
3 {
4 Cout <value <Endl;
5 }
6
7 Template <typename head, typename... rail>
8 Void Print (Head head, rail... rail)
9 {
10 Cout " , " ;
11 Print (rail ...);
12 }
13
14 Int Main ( Int Argc, Char * Argv [])
15 {
16 Print ( 1 ); // Output: 1
17 Print ( 1 , " Hello " ); // Output: 1, hello
18 Print ( 1 , " Hello " , ' H ' ); // Output: 1, hello, h
19 Return 0 ;
20 }
In the code above, we first define a function template with only one template parameter, which simply outputs the value of the input parameter. Then, a Variable Parameter Function template is defined. It outputs the value of the first parameter and calls itself recursively. Note that rail... is written in this way. It means to split the function parameter package into one parameter and pass it into print. In this way, the first parameter in the function parameter package is passed to the head, and the remaining parameters re-constitute a function parameter package and pass it to rail. When there is only one parameter in the recursive call to the function parameter package, the print function with only one template parameter is called.
After understanding the usage principles of variable template parameters, let's write our own printf function.
1 Void Myprintf ( Const Char * Psztext)
2 {
3 Assert (psztext! = NULL );
4
5 Cout <psztext;
6 }
7
8 Template <typename T, typename... ARGs>
9 Void Myprintf ( Const Char * Psztext, T value, argS... ARGs)
10 {
11 Assert (psztext! = NULL );
12
13 While (* Psztext)
14 {
15 If (* Psztext = ' % ' & Amp; * ++ psztext! = ' % ' )
16 {
17 Cout <value;
18 Myprintf (++ psztext, argS ...);
19 Return ;
20 }
21 Cout <* psztext ++;
22 }
23 }
The reasoning process when calling the myprintf function is similar to that of print. In addition, if you want to learn more about Variable Parameter templates, you can also refer toSource codeOr implement a simplified tuple.
Introduce new features of C ++ 11ArticleList:
1. [original] C ++ 11 right value reference
2. [original] C ++ 11 lambda expressions
3. [original] C ++ 11 Auto & decltype
4. [original] C ++ 11 perfect forwarding
5. [original] C ++ 11 syntax dessert
6. [original] C ++ 11 tuple & Variable Parameter Template