模板(一) 模板及相关概念的介绍

一、元编程(Meta Programming)

Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse or transform other programs, and even modify itself while running. In some cases, this allows programmers to minimize the number of lines of code to express a solution (hence reducing development time), or it gives programs greater flexibility to efficiently handle new situations without recompilation.

                                                                                                                                                                                                                                    —— wiki

在C语言中宏与C++中的模板都属于元编程的一种技术,其都通过在编译器在编译时对模板进行分析来产生其他代码。

 

二、泛型编程(generic programming)

In the simplest definition, generic programming is a style of computer programming in which algorithms are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by ML in 1973, permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplication. Such software entities are known as generics in Ada, Delphi, Eiffel, Java, C#, F#, Swift, and Visual Basic .NET; parametric polymorphism in ML, Scala and Haskell (the Haskell community also uses the term "generic" for a related but somewhat different concept); templates in C++ and D; and parameterized types in the influential 1994 book Design Patterns. The authors of Design Patterns note that this technique, especially when combined with delegation, is very powerful but that "[dynamic], highly parameterized software is harder to understand than more static software.

                                                                                                                                                                                                                                    —— wiki

在C++中,面向对象编程与模板元编程都能处理在编写程序时不清楚数据类型的情况;不同之处在于:OOP能够处理类型在程序运行之前都未知的情况;

而在在泛型编程中,在编译时期就能获知类型;

 

三、C++模板技术

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types.

This allows a function or class to work on many different data types without being rewritten for each one.

                                                                                                                                                                                                                                    —— wiki

模板是C++中泛型编程的基础,它允许编写与类型无关的函数与类;一个模板是创建函数或类的公式,通过模板编写类型无关的代码,再在编译时期通过获知

使用模板的具体数据的类型来生成针对该类型的具体的的类或者函数,这样可以避免对于每一种具体的数据类型重复定义类或者函数。

你可能感兴趣的:(模板)