Generic programming and new thinking of design

Source: Internet
Author: User
Tags abstract int size

Objective

Always keep in mind that the purpose of writing code is to be simple, not to use the obscure features of the language, to be clever, to write the code you understand, to understand the code you write, so that you may do better.

In the 1998, the International C + + standard was formally adopted, and the most important contribution of standardization to C + + was to give stronger support to "powerful abstract concepts" to reduce the complexity of software, and C + + provided two powerful abstract methods: Object-oriented programming and generic programming. Object-oriented programming everyone must be familiar with this, and there will be no more shivering. When it comes to generic programming (Generic programming), some people may not be familiar with it, but when it comes to STL, you'll hear about it. STL (Standard Template Library, Standard template gallery) is actually the implementation of generic programming, STL is by Alexander Stepanov (the father of the STL), David R Musser and Meng Lee three Masters of Common development, was incorporated into the C + + standard library in 1994. Although the STL joins the C + + standard library relatively late, it is the most revolutionary part of the C + + standard library, and it is also the most important part of the C + + standard library. Because almost everything in the new C + + standard library is made up of templates (Template), of course, the STL is no exception. Therefore, it is necessary to outline the relevant concepts of the template first.

Template Concepts

Using templates enables programs to have better code reusability. Remember that templates are reused for source code, rather than reusing object code through inheritance and composition, and when users use templates, the parameters are replaced by the compiler. A template consists of a class template and a function template two parts, a class that is called a class template with a description of the data type being processed, and a function that takes the description of the data type being processed as a function template. A template parameter can consist of a type parameter or a non type parameter, which is indicated by the class and TypeName keywords, both of which have the same meaning, indicating that the following parameter names represent a potential built-in or user-defined type, and that the untyped parameter is composed of a generic parameter declaration. The following are simple uses of class templates and function templates:

template<class T1, int Size>
class Queue    // 类模板,其中T1为类型参数,Size为非类型参数
{
public:
  explicit Queue():size_(Size){};    // 显式构造,避免隐式转换
  ……
  template<class T2> void assign(T2 first,T2 last);  // 内嵌函数模板
private:
  T* temp_;
  int size_;
}
  // 类模板中内嵌函数模板Compare的外围实现(如在Queue类外实现)
  template<class T1,int Size> template<class T2>
  void Queue<T1,Size>::assign (T2 first,T2 last) {};
  // 模板的使用方法
  int ia[4] = {0,1,2,3};
  Queue<int, sizeof(ia)/sizeof(int)> qi;
  qi.assign(ai,ai+4);

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.