1. Template
- Not all member functions of template class are instantiated. Only those used functions should be instantiated to same time and space.
- Compiler may not support all operators used in member function
Point<float> *p = new Point<float>;
//Only
//1. float type instance of template class
//2. new operator
//3. default constructor
//will be instantiated.
- scope of the template definition
- scope of the template instantiation
Member function instantiation - 3 points need to resolve:
- How to find the definition of function?
- How to instantiate only member funtions which are invoked?
- How can compiler prevent instantiate member definitions in more than one .o file
2. Exception Handling
compiler build up data structure to support exception in
- runtime - not expand the size of program but costs some efficiency
- compilation - increase the size of program but may be fast
How to support exception handling:
- check the function where throw happens
- see if throw is in a try block
- if so, compiler will check catch senteces
- if it matches, catch block will take control
- if throw is not in a try block or no catch found, then system should
- destroy all local active objects
- unwind current function from stack
- go to next function in the stack and repeat 2-5
3. Runtime Type Identification, RTTI
Overhead of doing cast in C++:
- additional space to store type_info object in vtbl
- additional time cost to decide runtime type
Dynamic cast is safe, return 0 if cast fails. The cost of dynamic cast is that compiler will create a type descriptor and it can be accessed through vptr.
((type_info*)(pt->vptr[0]))->_type_descriptor;
Typeid operator
class TestClass{};
TestClass temp;
typeid(temp).name(); // "class TestClass"
typeid(temp).raw_name(); // ".?AVTestClass" - decided by compiler
Pointer and reference:
// nullptr, no object is pointed to.
Point<float> *ptr = 0;
// it will instantiate a point instance
// which will be converted to
// Point<float> temp(float(0));
// const Point<float> &ref = temp;
const Point<float> &ref = 0;