template specialization vs template instantination

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    template specialization vs template instantination

    what is the difference between
    template specialization vs template instantination in C++ ?

    thanks in advance...
  • OuaisBla
    New Member
    • Jun 2007
    • 18

    #2
    template instanciation is when you give a to a template class a template parameter.

    Like:

    Code:
    std::vector<string> v;
    v is an instanciation of a template.

    Specialization is more complex but let me try to explain it simply.

    Code:
    template<class T>
    class vector { };  //base template
    
    template<class T>
    class vector<T *>   //partial specialization
    { };
    
    template<>
    class vector<string>   //complete specialization
    { };
    The compiler will choose each implentation is the better one regarding the template paramameters. Each specialization is a complete distinct class from the the base template. But this allows optimization and/or custom behaviour regarding different type using the same syntax.

    Code:
    vector<int> vi; //defaut template is used
    vector<int *> vip; //vector<T*> is used
    vector<string> vs; //vector<string> is used
    this offers lots of room for optimization, but for a vector user it is always a vector that is used.

    I hope this helps.

    Comment

    Working...