关闭

模板学习笔记(一)

581人阅读 评论(0) 收藏 举报

   C++的template是实现在库设计和嵌入式设计中的关键。
template能实现抽象和效率的结合;同时template还能有效地防止代码膨胀。主要原因:template分为definition和instantiation两个部分,而instantiation只是在上下文中用到才进行。

取决于为c++template参数设计的类型系统。简单有效地表达概念定义同时又足以在现代编译器中实现。集中在c++template概念周围,基本概念包括类型系统,支持参数多态---局部类型推导和依赖名字标识扩展。接近最优的性能的iso c++ template是以template definition 和 template uses 弱分离为代价。
template-parameters (explicitly mentioned
in the template declaration) and dependent names inferred
from the definition of the template (based on the fact
that their meanings depend on template parameters).
flexibility---libraries
template---generic programming--c++ parameterization mechanism
导致令人难解的错误消息
Using concepts, type-checking of template definitions is separated from their uses, thereby making templates easier to use and easier to compile.
template--支持 generic programming, generative programming, template metaprogramming
compile-time type computation 
provide abstraction without performance degradation
(inliner)
c++ libraries are becomming more powerful, more flexible, and more expressive.----但这将导致实现的复杂性
一般性--抽象性
generic programming --- algorithm oriented
The generic programming process derives generic algorithms from
families of concrete (non-generic) implementations that exhibit
some commonality.
函数式编程思想
编程最终还是会回归到数学
Generic Programming is concerned with building highly efficient algorithms.
Consider the problem of storing objects in containers and writing algorithms to manipulate such objects.
The STL solution is based on parameterizing containers with their element types and on completely separating the algorithms from the containers.
The basic idea is that you can consider the elements of any container as a sequence of elements.
the first STL rules and principles generalize beyond belief.
based directly on the hardware model of memory and computation.
Function objects are the C++ mechanism for higher-order constructs.
The techniques based on the use of templates and largely inspired by techniques from functional programming are qualitatively different from traditional data abstraction.
using templates – to be statically type safe and efficient.
Templates are the key to embedded systems programming and high-performance numeric programming where resources management and correctness are key.
the use of iterators (and allocators) to separate logical memory access from actual memory access is key to many high-performance numeric techniques and the use of small, easily inlined, objects are key to examples of optimal use of hardware in embedded systems programming.
        c++ template从另一种角度--函数编程对问题世界进行抽象,参数化类型是template的形式上特点,函数对象则扩大了传统函数的理解,也模糊了函数与对象的边界。抽象的过程则从对象世界转移到一些基元的构造(vector, list, string, map,set等)和操作的概念层次--算法,实现数据结构和算法的最大程度的重用,而且不失效率、灵活性以及接口的易用性。在template的定义层次上类型检查的暂时缺失,导致在只有在使用时才能调试的问题,会导致许多可读性差的提示信息,c++0x则主要在concept上对这个问题进行了深入探讨,并形成了ConceptGCC编译器支持。
      从抽象层度上,template更高,在结构和操作上的抽象,而类是在结构和操作上的组织。普通的函数指针参数不可能带其他信息,而函数对象则可根据需要带初始或运行时信息,方便了函数调用适时调用。
      c++是一种强类型语言,它的发展从类型抽象、运行时确定到泛型抽象、编译时确定,很奇特。而在实际运用中,又往往只是注重其中的一个方面,即便是大师也有他的局限。发展过程似乎有一种回归感,说到这里很容易想到http://blog.csdn.net/turingbook/archive/2007/09/07/1775488.aspx关于《c与c++》间的争论,其中不乏思想灵光,也第一次让我看到关于语言的争论一点意义所在。template技术在抽象和效率上的关注使其成为c++不可或缺的组件,也是c++进一步发展的强大推力。
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:196417次
    • 积分:2611
    • 等级:
    • 排名:第13941名
    • 原创:49篇
    • 转载:39篇
    • 译文:2篇
    • 评论:61条
    最新评论