Thursday, August 4, 2016

Terminology of Templates


Terminology of Templates

template<typename T>class Stack{public:    void push(T val);    T pop();    bool isEmpty() const;
private:    std::vector<T> mStack;};
    Stack<int> mStackInt;
,,,,
00  Template  Parameters
      T

01  Template Arguments
      int

02 Instantiation
      Stack<int> mStackInt;

03  Implicit Instantiation


04  Emplicit Instantiation


05  Lazy Instantiation


06  Specialization


07  Partial Specialization


Pros and Cons of Templates



Pros
  • It provides us type-safe, efficient generic containers and generic algorithms
  • The main reason for using C++ and templates is the trade-offs in performance and maintainability outweigh the bigger size of the resulting code and longer compile times.
  • The drawbacks of not using them are likely to be much greater.


Cons
  • Templates can lead to slower compile-times and possibly larger executable.
  • Compilers often produce incomprehensible poor error diagnostics and poor error messages.
  • The design of the STL collections tends to lead to a lot of copying of objects. The original smart pointer, std::auto_ptr, wasn't suitable for use in most collections. Things could be improved if we use other smart pointers from boost or C11.

















































No comments:

Post a Comment