Re: Help: How many explicit specializations required?

From: CoolPint (coolpint_at_yahoo.co.uk)
Date: 09/18/04


Date: 17 Sep 2004 22:03:03 -0700


> > template <typename T>
> > void func(T * const &) // should handle T * as well as const T *
> > { cout << "Overloading for Pointers\n"; }
> >
> > template <>
> > void func(char * const &)
> > { cout << "specialization: char * \n"; }
>
> This (taken as an example) will not work, spezialization (explicit or
> partial) is only possible with clases.
> There are two ways you can work around this:
> 1) Write a class where the function you need is a publc static member,
> then you can do this what you want
> 2) Work with overloadings, this should work fine, because template and
> non template functions are overloadable

Now you raised an issue I am quite confused about. I read in books
that there cannot be partial specialization of function templates, but
explicit specializations of function templates are allowoed.

>> template <>
>> void func(char * const &)
>> { cout << "specialization: char * \n"; }

According to the books, the above seems to be a valid explicit
specialization of the primary template. But you seems to suggest
that's not the case. I am confused with the terms. So is the above
case of explicit specialization or overloading?

I read that function templates cannot have partial specialization but
there is something called partial ordering whose evaluation method is
just like partial specialization of class templates.

> > template <typename T>
> > void func(T * const &) // should handle T * as well as const T *
> > { cout << "Overloading for Pointers\n"; }

I think the above case is a partial ordering (overloading of the
primary template with another template?) or is it? I am quite
confused.
What I understand is that because above overloaded template is more
"specialized" than the primary template, this template will be used to
instantiate a function when the argument is T *, then the resulting
instantiation will be entered into the list of candidate functions. Am
I understanding correctly?

How about the two below? Are they partial ordering of the primary
(overloading of templates with another templates)?

template <typename T, int N>
void func( T (&) [N] )
{ cout << "Overloading for T [] \n"; }

template <typename T, int N>
void func( const T (&) [N] )
{ cout << "Overloading for const T [] \n"; }

When I have a number of function templates whose names are same, will
compilers try to select only one of them from which to make a
instantiation?
Or will compilers make several instantiations from differente
templates and enter them all into the list of candidate functions?

> Or you can still use typetrais and overload the function too for the
> spezial int cases which you mentioned, which is IMHO the slightest
> effort to reach your aim.

Thank you for pointing this out. I just started reading about traits
and it seems a wonderful idea. Thanks again.



Relevant Pages

  • Re: LoadString overwrite
    ... doing some special string replacements. ... In MFC 8.0 the CString class is a template class and I couldn't figure ... Overloading it, you get a C2084 (explicit specialization; ...
    (microsoft.public.vc.mfc)
  • Re: LoadString overwrite
    ... In MFC 8.0 the CString class is a template class and I couldn't figure ... Overloading it, you get a C2084 (explicit specialization; ... You might try suppressing the instantiation using the "extern template" ... play linker games to replace member functions. ...
    (microsoft.public.vc.mfc)
  • LoadString overwrite
    ... doing some special string replacements. ... In MFC 8.0 the CString class is a template class and I couldn't figure ... Overloading it, you get a C2084 (explicit specialization; ...
    (microsoft.public.vc.mfc)
  • Re: generic programming is very powerful. c should support it
    ... If you make the template parameters part of the function's name, ... If there is a fully specialized template for these parameters, ... from interactions with overloading and inheritance. ...
    (comp.lang.c)
  • Re: dllexport vs. template + inheritance
    ... template instances. ... then the compiler wants to ... template is meant to be used with explicit instantiation ... It seems that exporting a class ...
    (microsoft.public.vc.language)