STL is an important part of the C ++ standard library. It is not only a reusable component library, but also a software framework that contains algorithms and data structures, it is also a good example of C ++ generic programming. Many advanced C ++ technologies are used in STL. This topic describes how to use real-time parameter inference for a template. Mainly refer to "C ++ primer" and "STL source code analysis".
STL is implemented using template templates. For example, its algorithms are all function templates. We know that a template is a formula or blueprint. It is not a class or function and needs to be instantiated. This process is completed during the compilation period. The Compiler deduce the type of the parameter based on the passed real parameters and instantiate the corresponding function. Definition in C ++ Primer: the process of determining the type and value of the template's real parameters from the function arguments is called template argument deduction ). The following describes several algorithms in STL. The names are modified and some code is omitted.
[CPP]
View plaincopyprint?
- // Small value
- Template <class T>
- Inline const T & min (const T & A, const T & B ){
- Return B <? B:;
- }
- // Relatively small
- Template <class T>
- Inline const T & MAX (const T & A, const T & B ){
- Return a <B? B:;
- }
// Small value template <class T> inline const T & min (const T & A, const T & B) {return B <? B: A;} // compared with the nested template <class T> inline const T & MAX (const T & A, const T & B) {return a <B? B: ;}
In the following scenarios, the compiler will instantiate two versions of min based on real parameters.
[CPP]
View plaincopyprint?
- Int main ()
- {
- Cout <min (1, 2) <Endl;
- Cout <min (1.2, 3.2) <Endl;
- Return 0;
- }
int main(){cout<<Min(1,2)<<endl;cout<<Min(1.2,3.2)<<endl;return 0;}
[CPP]
View plaincopyprint?
- Inline const Int & min (const Int & A, const Int & B ){
- Return B <? B:;
- }
- Inline const double & min (const double & A, const double & B ){
- Return B <? B:;
- }
inline const int& Min(const int& a, const int& b) { return b < a ? b : a;}inline const double& Min(const double& a, const double& b) { return b < a ? b : a;}
The description above is relatively simple. For details, see C ++ primer. This article only paves the way. In the previous article "C ++ technology used by STL (2)", when obtaining the data type referred to by the iterator, the template real-parameter inference mechanism is not used, instead, the built-in type technology and template special technology are used. The reason is that it cannot infer the type of the returned value. Here I will paste this code again.
[CPP]
View plaincopyprint?
- // Real Exchange Functions
- Template <class iter1, class iter2, class T>
- Inline void _ iter_swap (iter1 A, iter2 B, T ){
- T TMP = *;
- * A = * B;
- * B = TMP;
- }
- // Exchange the elements referred to by two iterators
- Template <class iter1, class iter2>
- Inline void iter_swap (iter1 A, iter2 B ){
- _ Iter_swap (a, B, * A); // real parameter inference of the template
- }
// The Real Exchange Function template <class iter1, class iter2, class T> inline void _ iter_swap (iter1 A, iter2 B, T) {T TMP = *; * A = * B; * B = TMP;} // exchange the element template <class iter1, class iter2> inline void iter_swap (iter1 A, iter2 B) referred to by the two iterators) {_ iter_swap (a, B, * A); // real parameter inference of the template}
The iter_swap function uses the real parameter inference of the template to obtain the data type referred to by the iterator. But what if I want this function to return the data of the first iterator? The real parameter inference of the template is powerless. You can use the embedded type, as defined below. Note that the keyword typename must be added to indicate that the compiler is of a type. Otherwise, compilation fails.
[CPP]
View plaincopyprint?
- # Include <iostream>
- # Include <vector>
- # Include <list>
- Using namespace STD;
- // Extraction agent
- Template <class I>
- Struct iterator_traits {
- Typedef typename I: value_type;
- };
- // Native special pointer
- Template <class T>
- Struct iterator_traits <t *> {
- Typedef t value_type;
- };
- // Special native regular pointer
- Template <class T>
- Struct iterator_traits <const T *> {
- Typedef t value_type;
- };
- // Real Exchange Functions
- Template <class iter1, class iter2, class T>
- Inline void _ iter_swap (iter1 A, iter2 B, T ){
- T TMP = *;
- * A = * B;
- * B = TMP;
- }
- // Exchange the elements referred to by two iterators
- Template <class iter1, class iter2>
- Inline typename iterator_traits <iter1>: value_type // obtain the type of the returned value using the embedded type
- Iter_swap (iter1 A, iter2 B ){
- _ Iter_swap (a, B, * A); // real parameter inference of the template
- Return *;
- }
- Int main ()
- {
- List <int> L;
- L. push_back (3 );
- L. push_back (4 );
- Cout <iter_swap (L. Begin (), ++ L. Begin () <Endl;
- Return 0;
- }
# Include <iostream> # include <vector> # include <list> using namespace STD; // extraction template <class I> struct iterator_traits {typedef typename I: value_type ;}; // special native pointer template <class T> struct iterator_traits <t *> {typedef t value_type ;}; // special native regular pointer template <class T> struct iterator_traits <const T *> {typedef t value_type ;}; // real exchange function template <class iter1, class iter2, class T> inline void _ iter_swap (iter1 A, iter2 B, T) {t tmp = * A; * A = * B; * B = TMP ;} // exchange the element template <class iter1, class iter2> inline typename iterator_traits <iter1>: value_type // obtain the type iter_swap (iter1, iter2 B) {_ iter_swap (a, B, * A); // returns * A;} int main () {list <int> L; L. push_back (3); L. push_back (4); cout <iter_swap (L. begin (), ++ L. begin () <Endl; return 0 ;}
I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985