 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Andy Guest
|
Posted: Sun Jan 30, 2005 9:34 am Post subject: What's the difference betwwen explicit instantiaion and expl |
|
|
Hi,
I got a little confused on 'instantiation' and 'specialization',
espcially for explicit instantiation and explicit sepcialization. Can
anybody explain the difference?
Thanks a lot!
Andy
|
|
| Back to top |
|
 |
Andy Guest
|
Posted: Sun Jan 30, 2005 9:40 am Post subject: Re: What's the difference betwwen explicit instantiaion and |
|
|
Is an instantiation a process of producing a specialization?
Thanks,
andy
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sun Jan 30, 2005 9:49 am Post subject: Re: What's the difference betwwen explicit instantiaion and |
|
|
* Andy:
| Quote: |
I got a little confused on 'instantiation' and 'specialization',
espcially for explicit instantiation and explicit sepcialization. Can
anybody explain the difference?
|
You specialize a template when you provide a definition for some actual
template parameter, e.g., saying "in general, use the general definition,
but for template parameter T=int, use this more specialized definition".
This specialized definition may or may not be actually used, so
specialization by itself does not cause a more thorough analyzis than
the original template definition.
You instantiate a template when you cause the compiler to generate code for
the template. This requires all template parameters given actual parameters,
and it causes full-blown analysis of this specialization of the template.
For example, you declare an object of the template type, or you call a
templated function. At that point the template definition is expanded to
actual, ordinary C++ code (the template instance), which is subject to the
usual C++ compilation (OK, some rules, e.g. about multiple definitions, are
slightly different). You can also instantiate a template without using it, via
special syntax.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
Shezan Baig Guest
|
Posted: Sun Jan 30, 2005 4:27 pm Post subject: Re: What's the difference betwwen explicit instantiaion and |
|
|
Andy wrote:
| Quote: | Hi,
I got a little confused on 'instantiation' and 'specialization',
espcially for explicit instantiation and explicit sepcialization.
Can
anybody explain the difference?
Thanks a lot!
Andy
|
If I have a templated class:
template <typename FOO> class Bar { ... };
I explicitly *instantiate* Bar<char> like this:
template <> class Bar<char>;
Notice that the implementation of Bar<char> does not change (i.e., it
is not specialised).
When I *specialise* Bar<char>, I do this:
template <> class Bar<char> { ... };
Now, Bar<char> has a new meaning (i.e., the template class 'Bar' now
does something "special" when used with 'char' as the template
argument).
Hope this helps,
-shez-
|
|
| Back to top |
|
 |
Max M. Guest
|
Posted: Sun Jan 30, 2005 4:36 pm Post subject: Re: What's the difference betwwen explicit instantiaion and |
|
|
Shezan Baig wrote:
| Quote: |
I explicitly *instantiate* Bar<char> like this:
template <> class Bar<char>;
|
The correct syntax for explicit instantiation is
template class Bar<char>;
(with no '<>'.)
MM
|
|
| Back to top |
|
 |
Shezan Baig Guest
|
Posted: Sun Jan 30, 2005 11:46 pm Post subject: Re: What's the difference betwwen explicit instantiaion and |
|
|
Max M. wrote:
| Quote: | Shezan Baig wrote:
I explicitly *instantiate* Bar<char> like this:
template <> class Bar<char>;
The correct syntax for explicit instantiation is
template class Bar<char>;
(with no '<>'.)
MM
|
Yes, this is correct. My bad.
-shez-
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|