By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

template specialization vs template instantination

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

thanks in advance...
Jun 21 '07 #1
Share this Question
Share on Google+
1 Reply


P: 18
template instanciation is when you give a to a template class a template parameter.

Like:

Expand|Select|Wrap|Line Numbers
  1. std::vector<string> v;
v is an instanciation of a template.

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

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class vector { };  //base template
  3.  
  4. template<class T>
  5. class vector<T *>   //partial specialization
  6. { };
  7.  
  8. template<>
  9. class vector<string>   //complete specialization
  10. { };
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.

Expand|Select|Wrap|Line Numbers
  1. vector<int> vi; //defaut template is used
  2. vector<int *> vip; //vector<T*> is used
  3. 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.
Jun 21 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.