Templates, specialization and compilers C++ conformance

After some hours of experiments with template specialization, I came to these conclusions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
   Conclusions:

   (at least with GCC:)

   0. must #include templates with their
      implementation.

   1. it's enough to use a template class in multiple
      translation units, if one or more of these are
      true:
      * class has no specialization;
      * class has specialization, but all of it's
        methods are inline;
      * class has specialization, it's non-inline
        methods get compiled in a separate file.

   2. it's enough to use a function template in
      multiple translation units, if one or more of
      these are true:
      * it has no specialization;
      * it has specialization, which gets compiled
        in a separate file.

   3. to make a specialization of a particular
      method of class template, specialization
      has to be be compiled in a separate file.
      Edit: for using into multiple translation units.

   4. all specializations start with "template <>",
      but this statement appears only once, that
      is:
         * at class specialization, but not at it's
           separate method implementations;
         * at function specializations.
 */

The reasons why I post them here are these:

A] I wonder, how it might change for other compilers than GCC (and for one, maybe you would like to add or correct something)?

B] Which C++ compilers actually support all C++ standard and how great variations of valid templates language features can be expected in the other few, which can be counted?
(well, haven't been experimenting with that, but who knows, someone here has? my sources just say, that there can be 'problems with templates' etc)
Last edited on
You're best going to the standard for the definition what a template is and its constraints.

2 and 3 are not correct.
i ever tried to learn the template,but i found out that there are so much constraints or say, secrets in it.
now , i seldom use template in my code because my lack of familiarization with it. May you guys sort it out .
It is tricky to begin with and there is always the question "What can I get away with?", but C++ templates are a powerful mechanism who's limits are beyond what you'd initially imagine.

Don't let the initial apparent complexity put you off.
kbw, how about 2 and 3 now?

And which compilers currently support all C++ standard? (or what's the point of knowing all about templates..)
Standard compliance has always been an issue. There is always some feature that a vendor has yet to implement or yet to implement correctly.

All C++ features are there for good practical reasons. If you defer using a relevant feature just because you suspect the vendor can't be asked to bother with it, you're only hurting yourself. If Turbo/Borland C++ hadn't introduced serious competition to Microsoft by implementing new features (like templates, exception handling, ...), it is conceivable that to this day Microsoft might not have implemented them. For example, it's only in Visual Studio 2005 that they correctly implemented for loop scope of variable lifetime.

I can't begin to describe how important templates are. An example that is common place is template use in STL to reduce the number of classes/functions in the library. Templates are used to reduce the number of containers, but by making those containers provide standard interfaces, template functions are used to reduce the number of functions that do the same sort of thing to those very different containers.

Topic archived. No new replies allowed.