C++ Knowledge series 5
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的更多相关文章
- Java Knowledge series 4
JVM & Bytecode Has-a or Is-a relationship(inheritance or composition) 如果想利用新类内部一个现有类的特性,而不想使用它的接 ...
- C++ Knowledge series 1
Programming language evolves always along with Compiler's evolvement. 1. The C++ Object Model: Strou ...
- C++ Knowledge series Template & Class
Function Function is composed of name, parameter (operand, type of operand), return value, body with ...
- 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 ...
- C++ Knowledge series Conversion & Constructor & Destructor
Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...
- C++ Knowledge series STL & Const
Thank to the pepole who devote theirself to the common libs. STL(http://www.cplusplus.com/reference/ ...
- Java Knowledge series 7
Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...
- C++ Knowledge series 2
Programming language evolves always along with Compiler's evolvement The semantics of constructors O ...
- Java Knowledge series 5
Interface from user, not from implementor.(DIP) Interface-Oriented Programming. Interface or Abstrac ...
- Java Knowledge series 3
JVM & Bytecode Abstract & Object Object in Java (1) 所有东西都是对象object.可将对象想象成一种新型变量:它保存着数据,但可要求 ...
随机推荐
- luogu4345 [SHOI2015]超能粒子炮·改(组合数/Lucas定理)
link 输入\(n,k\),求\(\sum_{i=0}^k{n\choose i}\)对2333取模,10万组询问,n,k<=1e18 注意到一个2333这个数字很小并且还是质数这一良好性质, ...
- Kbuild、Kconfig、make menuconfig、.config、Makefile之间的关系
今天突发奇想,想在这里分享下比喻分析Kbuild ---->去饭店吃饭的过程. 1.Kconfig --->饭店的菜单 2.条件编译选项--->菜单中的每一盘菜,可以选择这个菜的 ...
- CF796A Buying A House 模拟
Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains ...
- kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
- Palindromes _easy version(reverse)
Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串.请写一个程序判断读入的字符串是否是“回文”. Input 输入包 ...
- vue学习三:生命周期钩子
生命周期钩子介绍: 每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听.编译模板.将实例挂载到 DOM 并在数据变化时更新 DOM 等.同时在这个过程中也会运行一些叫做生 ...
- matplotlib学习笔记(三)
柱状图 柱状图用其每根柱子的长度表示值的大小,它们通常用来比较两组或多组值.下面的程序从文件中读入中国人口的年龄分布数据,并使用柱状图比较男性和女性的年龄分布. import numpy as np ...
- CSS(九)元素隐藏和利弊
隐藏元素.无法点击,这之间有什么关系呢? 可以配合我写的 html 食用:https://github.com/dirstart/ScriptOJ/blob/master/OJ_CSS/4.css%E ...
- 百度地图 api bug 解决.......
百度地图 遇到了一个默明奇妙的bug..... 调用后中心点 不再 point(标注的点上...)这是需要执行一次(仅一次) 当 地图 加载完后 执行(这个方法你每次改地图 都会执行...所以让他执 ...
- linux 下结合find 和 rm 删除大量文件
echo "remove only files, the logs folder and it contents will not be removed"find /opt/tra ...