C ++ technology used by STL (3) -- real parameter inference of the template.

Source: Internet
Author: User

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?
  1. // Small value
  2. Template <class T>
  3. Inline const T & min (const T & A, const T & B ){
  4. Return B <? B:;
  5. }
  6. // Relatively small
  7. Template <class T>
  8. Inline const T & MAX (const T & A, const T & B ){
  9. Return a <B? B:;
  10. }
// 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?
  1. Int main ()
  2. {
  3. Cout <min (1, 2) <Endl;
  4. Cout <min (1.2, 3.2) <Endl;
  5. Return 0;
  6. }
int main(){cout<<Min(1,2)<<endl;cout<<Min(1.2,3.2)<<endl;return 0;}
[CPP]
View plaincopyprint?
  1. Inline const Int & min (const Int & A, const Int & B ){
  2. Return B <? B:;
  3. }
  4. Inline const double & min (const double & A, const double & B ){
  5. Return B <? B:;
  6. }
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?
  1. // Real Exchange Functions
  2. Template <class iter1, class iter2, class T>
  3. Inline void _ iter_swap (iter1 A, iter2 B, T ){
  4. T TMP = *;
  5. * A = * B;
  6. * B = TMP;
  7. }
  8. // Exchange the elements referred to by two iterators
  9. Template <class iter1, class iter2>
  10. Inline void iter_swap (iter1 A, iter2 B ){
  11. _ Iter_swap (a, B, * A); // real parameter inference of the template
  12. }
// 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?
  1. # Include <iostream>
  2. # Include <vector>
  3. # Include <list>
  4. Using namespace STD;
  5. // Extraction agent
  6. Template <class I>
  7. Struct iterator_traits {
  8. Typedef typename I: value_type;
  9. };
  10. // Native special pointer
  11. Template <class T>
  12. Struct iterator_traits <t *> {
  13. Typedef t value_type;
  14. };
  15. // Special native regular pointer
  16. Template <class T>
  17. Struct iterator_traits <const T *> {
  18. Typedef t value_type;
  19. };
  20. // Real Exchange Functions
  21. Template <class iter1, class iter2, class T>
  22. Inline void _ iter_swap (iter1 A, iter2 B, T ){
  23. T TMP = *;
  24. * A = * B;
  25. * B = TMP;
  26. }
  27. // Exchange the elements referred to by two iterators
  28. Template <class iter1, class iter2>
  29. Inline typename iterator_traits <iter1>: value_type // obtain the type of the returned value using the embedded type
  30. Iter_swap (iter1 A, iter2 B ){
  31. _ Iter_swap (a, B, * A); // real parameter inference of the template
  32. Return *;
  33. }
  34. Int main ()
  35. {
  36. List <int> L;
  37. L. push_back (3 );
  38. L. push_back (4 );
  39. Cout <iter_swap (L. Begin (), ++ L. Begin () <Endl;
  40. Return 0;
  41. }
# 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

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.