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

What's the difference betwwen explicit instantiaion and explicit specialization?

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Is an instantiation a process of producing a specialization?
Thanks,

andy

Jul 23 '05 #2

P: n/a
* Andy:

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?
Jul 23 '05 #3

P: n/a

Andy wrote:
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-

Jul 23 '05 #4

P: n/a
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


Jul 23 '05 #5

P: n/a

Max M. wrote:
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-

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.