Re: Help: How many explicit specializations required?
From: CoolPint (coolpint_at_yahoo.co.uk)
Date: 09/18/04
- Next message: Sumit Rajan: "Re: Help with explicit instantiation"
- Previous message: Archer: "Re: google "top coder" contest = stacked against C++ coders"
- In reply to: Nicolas Pavlidis: "Re: Help: How many explicit specializations required?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Sumit Rajan: "Re: Help with explicit instantiation"
- Previous message: Archer: "Re: google "top coder" contest = stacked against C++ coders"
- In reply to: Nicolas Pavlidis: "Re: Help: How many explicit specializations required?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|