Programming language evolves always along with Compiler's evolvement

On the Cusp of the Object Model

Templates

  • Template are now the foundation for generic programming ( the Standard Template Library).
  • Template are used in memory allocation strategies, mutual exclusion mechanism for synchronizing thread, technique for template metaprograms.
  • Class expression templates are evaluated at compile time rather than runtime, thereby providing significant performance improvements.
  • This section focuses on the semantics of template support, a sort of when, why, and how of what is done with templates within a compilation system.

Template Instantiation

  • First, what happens when the compiler sees the template class declaration? In terms of the actual program, nothing. That is, the static data members are not available. Nor is the nested enum or its enumerators.
  • Although the actual type of the enum Status is invariant across all Point instantiations, as well as the value of its enumerators, each can be accessed only through a particular instance of the template Point class. Point< int>::enum variable = {};
  • This use of the static member causes an instance associated with the float instantiation of the Point class to be generated within the program.
  • Point< int>::static_variable = 99;
  • If we define a pointer to a particular instance, nothing happens in the program. Why? Because a pointer to a class object is not itself a class object; the compiler does not need to know anything about the layout or members of the class. Point< float > *ptr = 0; Just same as handle
  • The behavior in declaring a reference to a particular instance of a template, does result in the instantiation of a float instance of Point. Why? Because a reference cannot be an alias to "no object." The 0 is treated as an integer value that must be converted into an object of the type
  • Point< float > .If there is no conversion possible, then the definition is in error and is flagged at compile time.
  • Standard C++ requires that member functions be instantiated only if they are used (current implementations do not strictly follow this requirement). At compile-time.
  • There are two main reasons for the use-directed instantiation rule: 
    • 1. Space and time efficiency. If there are a hundred member functions associated with a class, but your program uses only two for one type and five for a second type, then instantiating the additional 193 can be a significant time and space hit.
    • 2. Unimplemented functionality. Not all types with which a template is instantiated support all the operators (such as i/o and the relational operators) required by the complete set of member functions. By instantiating only those member functions actually used, a template is able to support types that otherwise would generate compile-time errors.
  • The definition of origin, for example, requires the invocation of the default Point constructor and destructor. Only these two functions must be instantiated.
  • When defining a variable of class type, it involves calling its constructor and destructor within visible block, which is implemented by compiler.
  • It's interesting to note that although operator new is implicitly a static member of the class and so may not directly access any of its non-static members, it is still dependent on the actual template parameter type because its size_t first argument is passed the class size.
  • For one thing, all type-dependent checking involving the template parameters must be deferred until an actual instantiation occurs. It is different from class declaration.
  • In the original cfront implementation, the template is fully parsed but not type-checked. Type-checking is applied only to each particular instantiation. So under a parsing strategy, all the lexing and parsing errors are flagged during the handling of the template declaration.
  • Nonmember and member template functions are also not fully type-checked until instantiated.

Name Resolution within a Template

From:<<Inside C++ Object Model>>

C++ Knowledge series 5的更多相关文章

  1. Java Knowledge series 4

    JVM & Bytecode Has-a or Is-a relationship(inheritance or composition) 如果想利用新类内部一个现有类的特性,而不想使用它的接 ...

  2. C++ Knowledge series 1

    Programming language evolves always along with Compiler's evolvement. 1. The C++ Object Model: Strou ...

  3. C++ Knowledge series Template & Class

    Function Function is composed of name, parameter (operand, type of operand), return value, body with ...

  4. C++ Knowledge series Inheritance & RTTI & Exception Handling

    Inheritance The pointer or reference to base class can address/be assigned with any of the classes d ...

  5. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

  6. C++ Knowledge series STL & Const

    Thank to the pepole who devote theirself to the common libs. STL(http://www.cplusplus.com/reference/ ...

  7. Java Knowledge series 7

    Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...

  8. C++ Knowledge series 2

    Programming language evolves always along with Compiler's evolvement The semantics of constructors O ...

  9. Java Knowledge series 5

    Interface from user, not from implementor.(DIP) Interface-Oriented Programming. Interface or Abstrac ...

  10. Java Knowledge series 3

    JVM & Bytecode Abstract & Object Object in Java (1) 所有东西都是对象object.可将对象想象成一种新型变量:它保存着数据,但可要求 ...

随机推荐

  1. 洛谷4316 绿豆蛙的归宿(DAG递推/概率dp)

    题目大意: 给定一个DAG,求起点到终点的路径长度期望 根据题意可以知道每一条边都有一定概率被走到 那么\(\displaystyle\begin{aligned} Ans = \sum_{e \in ...

  2. asp.net core 简化模型验证 modelState.IsValid不用每一个写

    第一种:直接在执行action之前验证模型 实现 IActionFilter public class ModelStateFilter : IActionFilter { public void O ...

  3. Django 的 model form 组件

    Django 的 model form 组件 Model Form 组件的由来 之前介绍过 Django 的 Form 组件(Django的Form表单)使用方法,Form 组件能够帮我们做三件事: ...

  4. 华东交通大学2015年ACM“双基”程序设计竞赛1003

    Problem C Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Sub ...

  5. sqlserver 索引进阶(下)

    参考原文 http://www.cnblogs.com/tjy9999/p/4494799.html 第十级, 索引内部结构 建立索引的目的是加快对表中记录的查找或排序.为表设置索引要付出代价的:一是 ...

  6. JDK Integer

    1. public static int parseInt(String s, int radix) a. 充分考虑各种异常情况:字符串为空,带符号,进制出界,计算值出界 b. 计算时转换为负数进行处 ...

  7. Linux网络编程基础

    1. Linux网络模型 ① OSI七层模型和Linux四层模型 ② 各种协议之间的关系及在Linux模型中的位置 ③ 协议封装:各种协议处于一种层层封装的关系 (1)Ethernet (2)IP * ...

  8. Problem05 判断分数等级

    题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. 程序分析:(a>b)?a:b这是条件运算符的基本例子. impo ...

  9. php数组·的方法1-数组统计函数

    /** * 下面是数组统计函数 * * * **/ //count() 数组的长度 print_r(count($arr3)); echo '<hr>'; //max() min() 数组 ...

  10. 转 PyCharm 进行调试 以及怎么熟悉一个已经成熟的项目的代码和断点 以及 jetBrains pycharm快捷键

    https://blog.csdn.net/guider2334/rss/list Ctrl + Q 现实document视图,查看选择元素的详细信息        (重要) Ctrl + Alt + ...