Implementing and optimizing abstract operations with function templates

Source: Internet
Author: User
Tags abstract

Summary: This article describes the concept, use, and how to create function templates and function templates ...

When you create a function that completes an abstract operation, such as copy, reverse, and sort, you must define multiple versions so that you can handle each type of data. Take the Max () function as an example, which returns the larger of the two parameters:

double max(double first, double second);
complex max(complex first, complex second);
date max(date first, date second);
//..该函数的 其它版本

Although the implementation of this function is the same for different data types, programmers must define a separate version for each data type:

double max(double first, double second)
{
  return first>second? first : second;
}
complex max(complex first, complex second)
{
  return first>second? first : second;
}
date max(date first, date second)
{
  return first>second? first : second;
}

This not only repeat the work, error prone, but also bring a lot of maintenance and debugging work. Even worse, even if you don't use a version in your program, the code still increases the size of the executable file, and most compilers will not remove the unreferenced functions from the executable.

Using ordinary functions to implement abstract operations can force you to define multiple instances of the function, which incurs a lot of maintenance and debugging overhead. The workaround is to use a function template instead of a normal function.

Working with Function templates

The function template solves all of the above problems. Type independent and automatically instantiated only when needed. This article shows you how to define a function template to abstract common operations, demonstrate how to use them, and discuss optimization techniques.

First step: Define

The declaration of a function template is a parameter and a prototype that follows one or more templates in a template after the keyword has been read. As opposed to a normal function, it is usually declared in one conversion unit and defined in another, and you can define a template in a header file. For example:

// file max.h
#ifndef MAX_INCLUDED
#define MAX_INCLUDED
template <class T> T max(T t1, T t2)
{
  return (t1 > t2) ? t1 : t2;
}
#endif

<class t> defines T as a template parameter, or a placeholder, which replaces a specific data type when you instantiate Max (). Max is the function name, T1 and T2 are its arguments, and the return value is of type T. You can use this max () as you would with a normal function. The compiler automatically generates the appropriate template specificity according to the data type being used, or an instance:

int n=10,m=16;
int highest = max(n,m); // 产生 int 版本
std::complex<double> c1, c2;
//.. 给 c1,c2 赋值
std::complex<double> higher=max(c1,c2); // complex 版本

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.