C + + Template special case application _c language

Source: Internet
Author: User

Template special-case is a very important application in C + + programming, this article analyzes it with the example form, I believe that we can bring some help to understand the program design of C + +. The specific contents are as follows:

First, templates are a very important feature of C + +, and writing a piece of code can be used for a variety of data types (including user-defined types). For example, an STL sort () function can be used for sorting multiple data types, and class stacks can be used as a stack of multiple data types. But what if we want to execute different code (not a generic template) on a particular data type? In this case, you can use template exceptions (template specialization).

Special case of function template

When you instantiate a function template, you must provide an argument for each template parameter in the original template. Use the keyword template followed by an empty angle bracket <>, that is, the template <>, to indicate that we are taking a special example of a template.

Template <typename t> 
void Fun (T a) 
{ 
  cout << "The main template fun ():" << a << Endl ; 
} 
 
Template <>  //INT-type special-case 
void Fun (int a) 
{ 
  cout << specialized template for int type: "<& Lt A << Endl; 
} 
 
int main () 
{ 
  fun<char> (' a '); 
  Fun<int> (ten); 
  Fun<float> (9.15); 
  return 0; 
} 

Output results:

The main template fun (): A 
specialized template for int type:10 The 
main template fun (): 9.15 

For other data types other than int, the generic version of the function template fun (T a) is invoked, and for the int type, the special version of Fun (int a) is invoked. Note that the nature of a special edition is an instance, not a function overload . Therefore, the special case does not affect the function match.

Second, class template special case

In addition to the special functions of the template, we can also special-class template. The following is a simple example:

Template <typename t> 
class test{public 
: 
  void print () { 
    cout << ' General Template object ' << Endl; 
  } 
; 
 
template<>  //INT-type Special 
class test<int>{public 
: 
  void print () { 
    cout << " Specialized Template Object "<< Endl; 
  } 
}; 
 
int main () 
{ 
  test<int> A; 
  Test<char> b; 
  Test<float> C; 
  A.print (); 
  B.print (); 
  C.print (); 
  return 0; 
}

Output results:

Specialized Template Object 
template Object General 
template Object

In addition, unlike function templates, the exception of class templates does not have to provide arguments for all template parameters. We can specify only a subset rather than all of the template parameters, which is called the partial or partial exception of the class template (partial specialization). For example, the class vector definition in the C + + standard library is:

Template <typename T, typename allocator> 
class vector 
{ 
  /*......*/ 
}; 
 
Partially special 
template <typename allocator> 
class Vector<bool, allocator> 
{ 
  /*......*/ 
};

In the vector example, one parameter is bound to the bool type, while the other is still unbound and needs to be specified by the user. Note that a partially-instantiated version of a class template is still a template, because users must also provide arguments for template parameters that are not specified in the special edition .

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.