Re: More template issues
On 4/20/07 10:28 AM, in article 58s2hoF2hnvedU1@mid.individual.net, "Alf P.
Steinbach" <alfps@start.no> wrote:
1. Multiple explicit specializations.
The Holy Standard tells us, in its glorious ?14.7/5, that no /program/
shall specialize a template more than once for a given set of
template-arguments, but that a compiler doesn't need to diagnose
violations of this rule.
q1A Can it really be correct that (from an in-practice point of view)
explicit specializations can't be placed in header files, and expect the
code to compile with all standard-conforming compilers?
On the contrary: an explicit template specialization must be included by
each translation unit in which it would be selected to instantiate the
template (although no diagnostic is required for a violation of this rule,
see ?14.7.3/6). Typically, a programmer specializing a template will place
the specialization in a header file. In this way every source file that
might instantiate the template can include the header thereby ensuring that
any instantiation that matches the specialization will be sure to use the
same specialization (and thereby meet the requirement that there is only one
specialization with a particular argument list in the entire program.)
It is also important not to mistake an "explicit specialization" for an
"explicit instantiation". Because a program is allowed only one explicit
instantiation of a template specialization (and because each appearance of
an explicit instantiation declaration counts separately) - it is the case
that a program (for all practical purposes) must declare an explicit
instantiations in a source file.
q1B Since inline explicit function specializations are supported, how
about inline explicit function specializations?
Yes, inline function templates are supported. In fact, even though a
function template is usually declared in a header file, the functions it
instantiates are not inline functions. To instantiate inline functions a
function template must be declared with the "inline" type specifier.
Otherwise the functions instantiated by a function template - are not inline
functions.
2 Referring to a template as template.
In [comp.lang.c++] "Kostas", in the thread "MoreCRTP question", asked
about this code:
template<class T, template <typename> class D>
class Base
{
protected:
Base(T x): i(x) {}
T i;
};
template<class T>
class Derived:public Base<T, Derived>
{
Derived(T x):Base<T, Derived>(x){}
};
Compilers just plain refuse to treat "Derived" as referring to the
template: it's treated as referring to "Derived<T>".
"Zeppe" proposed the following solution:
Derived(T x):Base<T, ::Derived>(x){}
MSVC 7.1 accepts "class" instead of "::".
q2A Is the "::"-qualification standard-conforming in creating a
reference to the template as template, and if it is, how does that work,
exactly?
Yes, the global namespace qualifier is the correct way to disambiguate the
template name from the template's injected class name inside the template's
definition. This problem is discussed in ?14.6.1/2c along with an example of
this solution.
q2C What do we call "Derived<T>" (general term)?
Within its own template definition, Derived<T> is the injected class name of
the current instantiation. In other contexts, Derived<T> is also a
"template-id".
3 What's a specialization, formally.
"A specialization is a class, function, or class member that is either
instantiated or explicitly specialized." ?14.7/4
The most important thing to remember about a specialization is this: a
specialization - unlike a template - defines a type.
?14.7/4 states that a specialization is a class, function or class
member that is either instantiated or explicitly specialized, where the
previous paragraph effectively defines explicit specialization as
explicit full specialization.
Yes, a "specialization" is short for "full specialization." In fact the
"full" is somewhat redundant (see the next question).
q3A Isn't a partial specialization a specialization?
No. Unlike a specialization, a partial specialization is a template - and
not a type. The word "partial" in this context means that the specialization
is incomplete. In other words, although some type information has been
specified in the partial specialization, there is not enough type
information to complete the specialization and to transform a definition of
a template into a definition of a type.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]