C ++ Templates Reading Notes 1 _ FUNCTION Template

Source: Internet
Author: User

 

Templates are the basis of generic programming. The so-called generic programming is to write code independently and in any specific type.
Function template example:
[Cpp]
Template <typename T>
Inline const T & max (const T & a, const T & B)
{
T tmp = (a> B )? A: B;
Return tmp;
}

Template <typename T>
Inline const T & max (const T & a, const T & B)
{
T tmp = (a> B )? A: B;
Return tmp;
} The template definition starts with the keyword template, followed by the template parameter table. The template parameter table contains a list of one or more template parameters, separated by commas.
The type parameter is defined after the keyword class or typename. The type parameter is T.
The following operations use the function template:
[Cpp]
Int main ()
{
Printf ("ret = % f \ n",: max (11, 22 ));
Return 0;
}

Int main ()
{
Printf ("ret = % f \ n",: max (11, 22 ));
Return 0;
} The program instantiates a template instance of the corresponding type based on different parameter types. In the preceding example, if the parameter type is int, the program has the semantics of calling the code.

[Cpp]
Const int & max (const int & a, const int & B)
{
T tmp = (a> <STRONG> </STRONG> B )? A: B;
Return tmp;
}

Const int & max (const int & a, const int & B)
{
T tmp = (a> B )? A: B;
Return tmp;
} If two different types are used for the real parameters of a function call, an error occurs.
[Cpp]
: Max (1, 1.1); // error the first parameter type is int, and the second parameter type is double.

: Max (1, 1.1); // error the first parameter type is int, and the second parameter type is double. There are three ways to solve this error:
1. Forced type conversion for real parameters so that they can match each other
[Cpp] view plaincopyprint? : Max (static_cast <double> (1), 1.1 );

: Max (static_cast <double> (1), 1.1); 2. display the type of the specified T
[Cpp]
: Max (static_cast <double> (1), 1.1 );

: Max (static_cast <double> (1), 1.1); 3. specify two parameters with Different Types
See the following template definition:
[Cpp]
Template <typename T1, typename T2>
Inline const T2 & add (const T1 & a, const T2 & B)
{
T2 tmp = a + B;
Return tmp;
}

Template <typename T1, typename T2>
Inline const T2 & add (const T1 & a, const T2 & B)
{
T2 tmp = a + B;
Return tmp;
} The following call
[Html] view plaincopyprint? Int main ()
{
Printf ("ret = %. 2f \ n",: add (1, 1.1 ));
Return 0;
}

Int main ()
{
Printf ("ret = %. 2f \ n",: add (1, 1.1 ));
Return 0;
} If the template parameters are defined as follows:
[Cpp]
Template <typename T1, typename T2, typename T3>
Inline const T3 & add (const T1 & a, const T2 & B)
{
T3 tmp = a + B;
Return tmp;
}

Template <typename T1, typename T2, typename T3>
Inline const T3 & add (const T1 & a, const T2 & B)
{
T3 tmp = a + B;
Return tmp;
} The following call
[Cpp]
Int main ()
{
Printf ("ret = %. 2f \ n",: add (1, 1.1 ));
Return 0;
}

Int main ()
{
Printf ("ret = %. 2f \ n",: add (1, 1.1 ));
Return 0;
} Then the compiler will have an error. The compiler cannot implicitly assign values to T3. In this case, the specified T3 type must be displayed.
[Cpp]
Int main ()
{
Printf ("ret = %. 2f \ n",: add <int, double, double> (1, 1.1 ));
Urn 0;
}

Int main ()
{
Printf ("ret = %. 2f \ n",: add <int, double, double> (1, 1.1 ));
Return 0;
} Obviously, this call is too complicated. For T3, you can specify the type for it, while T1 and T2 can be interpreted based on the real parameter type.
That is to say, to change the Declaration Order of template parameters, only the specified first real parameter is displayed.
You must specify all real parameter types before "the last template real parameter that cannot be implicitly interpreted", as defined in the following template:
[Cpp]

Template <typename RT, typename T1, typename T2>
Inline const RT & add (const T1 & a, const T2 & B)
{
RT tmp = a + B;
Return tmp;
}

Template <typename RT, typename T1, typename T2>
Inline const RT & add (const T1 & a, const T2 & B)
{
RT tmp = a + B;
Return tmp;
} The following call

[Cpp]
Int main ()
{
Printf ("ret = %. 2f \ n",: add <double> (1, 1.1 ));
Return 0;
}

Int main ()
{
Printf ("ret = %. 2f \ n",: add <double> (1, 1.1 ));
Return 0;
} In this example, when you call add <double>, the RT is explicitly specified as double, but the other two parameters T1 and T2 can be interpreted as int and double respectively based on the actual call parameters.


Like common functions, function templates can also be overloaded.
A non-template function can coexist with a function template with the same name, and the function template can also be instantiated as this non-function template.


The following procedure:
[Cpp]
Namespace
{
Inline int const & max (int const & a, int const & B)
{
Printf ("max (int const & a, int const & B) \ n ");
Return a <B? A: B;
}
 
Template <typename T>
Inline T const & max (T const & a, T const & B)
{
Printf ("template <typename T> inline T const & max (T const & a, T const & B) \ n ");
Return a <B? A: B;
}
 
Template <typename T>
Inline T const & max (T const & a, T const & B, T const & c)
{
Printf ("template <typename T> inline T const & max (T const & a, T const & B, T const & c) \ n ");
Return: max (a, B), c );
}
 
}

Namespace
{
Inline int const & max (int const & a, int const & B)
{
Printf ("max (int const & a, int const & B) \ n ");
Return a <B? A: B;
}

Template <typename T>
Inline T const & max (T const & a, T const & B)
{
Printf ("template <typename T> inline T const & max (T const & a, T const & B) \ n ");
Return a <B? A: B;
}

Template <typename T>
Inline T const & max (T const & a, T const & B, T const & c)
{
Printf ("template <typename T> inline T const & max (T const & a, T const & B, T const & c) \ n ");
Return: max (a, B), c );
}

} There are the following calls: [cpp] view plaincopyprint? Int main ()
{
Std: cout <": max (7, 42, 68) =" <: max (7, 42, 68) <std: endl;
Std: cout <": max (7.0, 42.0) =" <: max (7.0, 42.0) <std: endl;
Std: cout <": max ('A', 'B') =" <: max ('A', 'B') <std :: endl;
Std: cout <": max (7, 42) =" <: max (7, 42) <std: endl;
Std: cout <": max <> (7, 42) =" <: max <> (7, 42) <std: endl;
Std: cout <": max <double> (7, 42) =" <: max <double> (7, 42) <std: endl;
Std: cout <": max ('A', 42.7) =" <: max ('A', 42.7) <std: endl;
Return 0;
}

Int main ()
{
Std: cout <": max (7, 42, 68) =" <: max (7, 42, 68) <std: endl;
Std: cout <": max (7.0, 42.0) =" <: max (7.0, 42.0) <std: endl;
Std: cout <": max ('A', 'B') =" <: max ('A', 'B') <std :: endl;
Std: cout <": max (7, 42) =" <: max (7, 42) <std: endl;
Std: cout <": max <> (7, 42) =" <: max <> (7, 42) <std: endl;
Std: cout <": max <double> (7, 42) =" <: max <double> (7, 42) <std: endl;
Std: cout <": max ('A', 42.7) =" <: max ('A', 42.7) <std: endl;
Return 0;
}, The output result is

 

[Cpp]
: Max (7, 42, 68)

: Max (7, 42, 68) calls the template function max () of the three parameters. According to the max () implementation, all three parameters are of the int type, therefore, the non-template function max () is called ().
[Cpp]
: Max (7.0, 42.0)

: Max (7.0, 42.0) because the two parameters are double, the template function max () of the two parameters is called ().
[Cpp]
: Max (7.0, 42.0)

: Max (7.0, 42.0) also called the template function.
[Cpp]
: Max (7, 42)

: Max (7, 42) for non-template functions and function templates with the same name, if other conditions are the same, when calling, the reload parsing process usually calls non-template functions without generating an instance from the template.
A non-template function is called.
[Cpp]
: Max (7, 42)

: Max (7, 42) explicitly calls the function template.
[Cpp]
: Max <double> (7, 42)

: Max <double> (7, 42) shows the called function template.
[Cpp]
: Max ('A', 42.7)

: Max ('A', 42.7) because the template does not allow automatic type conversion, but normal functions are acceptable, non-template functions are called. Convert 'A' and 42.7 to int
The following example will reload the template of the maximum value of the ball for the pointer and the normal C string.
[Cpp]
Namespace
{

Template <typename T>
Inline T const & max (T const & a, T const & B)
{
Printf ("template <typename T> inline T const & max (T const & a, T const & B) \ n ");
Return a> B? A: B;
}
 
Template <typename T>
Inline T const & max (T const & a, T const & B, T const & c)
{
Return: max (a, B), c );
}
 
Template <typename T>
Inline T * const & max (T * const & a, T * const & B)
{
Printf ("template <typename T> inline T * const & max (T * const & a, T * const & B) \ n ");
Return * a> * B? A: B;
}
 
Inline char const * const & max (char const * const & a, char const * const & B)
{
Printf ("inline char const * const & max (char const * const & a, char const * const & B) \ n ");
Return std: strcmp (a, B)> 0? A: B;
}
 
}

Namespace
{
 
Template <typename T>
Inline T const & max (T const & a, T const & B)
{
Printf ("template <typename T> inline T const & max (T const & a, T const & B) \ n ");
Return a> B? A: B;
}

Template <typename T>
Inline T const & max (T const & a, T const & B, T const & c)
{
Return: max (a, B), c );
}

Template <typename T>
Inline T * const & max (T * const & a, T * const & B)
{
Printf ("template <typename T> inline T * const & max (T * const & a, T * const & B) \ n ");
Return * a> * B? A: B;
}

Inline char const * const & max (char const * const & a, char const * const & B)
{
Printf ("inline char const * const & max (char const * const & a, char const * const & B) \ n ");
Return std: strcmp (a, B)> 0? A: B;
}

} There are the following calls:
[Cpp]
Int main (int argc, char ** argv)
{
Int a = 7;
Int B = 42;
Std: cout <: max (a, B) <std: endl; // calculates the maximum values of two int values.
 
Std: string s = "hey ";
Std: string t = "you ";
Std: cout <: max (s, t) <std: endl; // calculate the maximum values of the two std: string types.
 
Int * p1 = & B;
Int * p2 = &;
Std: cout <: max (p1, p2) <std: endl; // calculates the maximum value pointed to by the two pointers.
 
Char const * s1 = "David ";
Char const * s2 = "Nico ";
Std: cout <: max (s1, s2) <std: endl; // calculate the maximum value of two c strings
Return 0;
}

Int main (int argc, char ** argv)
{
Int a = 7;
Int B = 42;
Std: cout <: max (a, B) <std: endl; // calculates the maximum values of two int values.

Std: string s = "hey ";
Std: string t = "you ";
Std: cout <: max (s, t) <std: endl; // calculate the maximum values of the two std: string types.

Int * p1 = & B;
Int * p2 = &;
Std: cout <: max (p1, p2) <std: endl; // calculates the maximum value pointed to by the two pointers.

Char const * s1 = "David ";
Char const * s2 = "Nico ";
Std: cout <: max (s1, s2) <std: endl; // calculate the maximum value of two c strings
Return 0;
} The output result is as follows:

 


When you call an overload function, the call result may be related to whether the overload function is visible to this instance or not, but it may not.
In fact, define a max () version with three parameters, and the declaration of an overloaded max () version with two int parameters is not seen at the definition,
Then, the max () call with three int instances will use a template with two parameters, instead of using the int-based overloaded version max ().


Summary:
1. The template function defines a function family for different template parameters.
2. When you pass the real parameters of the template, You Can instantiate the function template based on the type of the real parameters.
3. You can display the specified template parameters.
4. You can reload the function template.
5. When you reload a function template, restrict your changes to the specified template parameters displayed.
6. Make sure that the declaration of all the overloaded versions of the function template is prior to the position where they are called.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.