Example of the simplest C ++ Template

Source: Internet
Author: User

  1. // Maxtemplate. cpp: defines the entry point for the console application.
  2. //
  3. # Include "stdafx. H"
  4. # Include <iostream>
  5. # Include <string>
  6. Using namespace STD;
  7. Template <typename T>
  8. Inline t const & MAX (T const & A, T const & B)
  9. {
  10. Return A> B? A: B;
  11. }
  12. // Test
  13. Int main ()
  14. {
  15. Int A = 0;
  16. Int B = 0;
  17. // Assign a value to a B as a random number
  18. A =: rand ();
  19. B =: rand ();
  20. Cout <"max (" <A <"," <B <") =" <: max (a, B) <Endl;
  21. Double Du1 = 0.0;
  22. Double du2 = 0.0;
  23. Du1 = (double) rand ()/10000;
  24. Du2 = (double) rand ()/10000;
  25. Cout <"max (" <Du1 <"," <du2 <") =" <: max (Du1, du2) <Endl;
  26. String str1 = "hello ";
  27. String str2 = "test ";
  28. Cout <"max (" <str1 <"," <str2 <") =" <: max (str1, str2) <Endl;
  29. Return 0;
  30. }

The program calls Max () three times. The first given independent variable is two int, the second given independent variable is two double, and the last given is two STD: string. Each max () value is greater than the two values. The program running result is: max (7, I): 42max (F1, F2): 3.4max (S1, S2): mathematics () the prefix ":" is added to all three calls to ensure that the called Max () is defined in the global namespace (). There is also a STD: max () template in the standard library, which may be called in some cases or lead to ambiguity (ambiguity, ambiguity) during the call ). In general, templates is not compiled into a single entity that can process any type, but is compiled into multiple individual entities, each of which processes a specific type. Therefore, for the three types, max () is compiled into three entities. For example, the first call of max (): int I = 42; max (7, I) uses the function template "using int as template parameter t", which is equivalent to calling the following function: inline int const & MAX (INT const & A, int const & B) {// If a <B, return B; otherwise, return areturn A <B? B: A;} the process of replacing template parameters with a specific type is called Instantiation or materialized 」). A template entity (Instance) is generated during the process ). Unfortunately, instantiation (instantiation, instantiation product) and instance (entity) terms have other meanings in the OO (Object-Oriented) programming field, it is usually used to represent a Class Object (concrete object ). This book deals with templates, so when we use this term, unless otherwise explicitly indicated, it expresses the meaning of templates. Note that the instantiation process is automatically triggered as long as the function template is used. Programmers do not have to apply for the instantiation process individually. Similarly, the other two calls to max () are instantiated as: const double & MAX (double const &, double const &); const STD: string & MAX (STD:: String const &, STD: String const &); if you try to instantiate a function template of a type that does not support the operations used in the function template, this will cause compilation errors. For example: STD: complex <float> C1, C2; // operator <max (C1, C2) is not provided for this type; // an error occurs during compilation. In fact, templates will be compiled twice: 1. if this parameter is not instantiated, you only need to perform a syntax check on the template program code to find syntax errors, such as missing semicolons. 2. During instantiation, the compiler checks whether all calls in the template program code are valid. For example, "unsupported function calls" will be checked at this stage. This causes a serious problem: when a function template is used to trigger the instantiation process, the compiler sometimes needs to use the original definition of the template. In general, for normal (non-template) functions, the compilation and link steps are independent. The Compiler only checks whether the declared statements of each functions are consistent with the called statements, however, template compilation destroys this rule. The solution is discussed in Chapter 6th. Now we can use the simplest solution: Write the template program code in the form of inline in the header file (header.

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.