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>>

最新文章

  1. 浏览器与web客户端的HTTP交互过程
  2. sed巧用,随手记
  3. Error #include nested too deeply
  4. Properties集合的练习
  5. Office2010安装错误
  6. C# - 重写虚方法
  7. 我在ubuntu桌面系统下进行WEB开发常用的软件
  8. Eclipse导入项目常见问题----facet版本问题04
  9. 201521123056 《Java程序设计》第1周学习总结
  10. [OC] UITableView 与 UItableViewCell 的使用
  11. 1.8 Double-Opening and Virtual Machine
  12. Linux 设置自启动服务
  13. mysql-约束介绍
  14. 【Python全栈-后端开发】Django进阶2-Form表单
  15. Selenium2+python自动化73-定位的坑:class属性有空格
  16. 【九天教您南方cass 9.1】 10 DTM土方计算的四种方法
  17. oracle创建索引表空间
  18. shell 数组使用简介
  19. mfc 重载赋值运算符
  20. C#怎样用文件读写在文件的原有基础上追加一行数据

热门文章

  1. P3749 [六省联考2017]寿司餐厅 最小割
  2. log4j配置文件及java调用 每个级别输出到不同的文件
  3. linux 基本概念
  4. docker的常用操作
  5. N - 嘤嘤嘤 (并查集+枚举)
  6. linux中swap的构建
  7. java——编译和运行
  8. Python Numpy Array
  9. IIS 配置asp.net 环境
  10. shell查看内存