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. P3348 [ZJOI2016]大森林

    \(\color{#0066ff}{ 题目描述 }\) 小Y家里有一个大森林,里面有n棵树,编号从1到n.一开始这些树都只是树苗,只有一个节点,标号为1.这些树都有一个特殊的节点,我们称之为生长节点, ...

  2. CF1096G Lucky Tickets 快速幂套FFT

    \(\color{#0066ff}{ 题目描述 }\) 一个\(n\)位数,每位可以是给出的\(k\)个数码中的一个数,可以有前导\(0\),输出前\(n/2\)位之和与后\(n/2\)位之和相等的方 ...

  3. JavaScript new Date()IE浏览器下出错 NaN

    当涉及到编程,与日期的工作可能是棘手的.然而,幸运的是,大多数语言都已经完成了这个困难的工作,并有一种内置的日期功能来帮助我们.JavaScript是具有有用的功能来帮助大量输出,设置日期. The ...

  4. Tensorflow方法介绍

    一.reduce系列函数(维度操作) 1.tf.reduce_sum( input_tensor, axis=None, keep_dims=False, name=None, reduction_i ...

  5. Java基础笔记(十八)——多态

    多态表示同一个操作作用在不同对象时,会有不同的结果. 多态可分为编译时多态和运行时多态. 编译时多态:方法重载,编译时就可以确定到底调用哪个方法,可以被看做一个类中的方法多态性. 运行时多态:只有在运 ...

  6. FMDB----SQL----数据库

    #pragma mark -- 数库 - (void)createDatabase{ //路径 NSString *path = [NSString stringWithFormat:@"% ...

  7. 2019-5-1 maven学习笔记

    一.maven的好处 同样的项目使用maven工程来实现,由于不需要导入很多jar包,源码很小 原理:根据坐标到项目的仓库里查找需要的依赖 二.安装步骤 1.到http://maven.apache. ...

  8. [題解](最小生成樹)luogu_P2916安慰奶牛

    可以發現每個點經過次數恰好等於這個點的度數,所以把點權下放邊權,跑最小生成樹,原來邊權乘二在加上兩端點權,答案再加一遍起點最小點權 #include<bits/stdc++.h> #def ...

  9. 二创建maven父子项目

    关于maven中的parent聚合一直都有没好好总结,固有这篇. ------------------------------------------------------------------- ...

  10. POJ1008 Maya Calendar

    题目来源:http://poj.org/problem?id=1008 题目大意: Maya人认为一年有365天,但他们有两种日历.一种叫做Haab,有19个月.前18个月每月20天,每个月的名字分别 ...