Topic: template function specialization
Author: "Sergey P. Derevyago" <ders.NOS@skeptik.net>
Date: 2000/08/01 Raw View
Steve Clamage wrote:
> > I need this feature in my real code. Now I have to use a wrapper class with
> > the static member function.
> You don't need partial specialization of function templates, because
> you can get the same effect with overloading.
Sorry, but I can't agree with you: I _really_ need partial specialization of
function templates. Here is my code:
template<int N>
inline void InsertionSort(int* a)
{
InsertionSort<N-1>(a+1);
// ...
}
// specialization
template<> inline void InsertionSort<1>(int*) {}
int main()
{
int a[]={3, 1, 2};
InsertionSort<3>(a);
}
Sorting of plain int is not very usable and I need more general:
template<int N, class Ran>
inline void InsertionSort(Ran a)
but with current C++ I'm not allowed to define appropriate specialisation:
template<class Ran> inline void InsertionSort<1, Ran>(Ran) {}
So I have to simulate this technique with a wrapper class and static member
function.
--
With all respect, Sergey. http://cpp3.virtualave.net/
mailto : ders at skeptik.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Sergey P. Derevyago" <ders.NOS@skeptik.net>
Date: 2000/07/27 Raw View
I'm allowed to write:
---f1.cpp--------------------------------------------------------------
template <int N>
inline void f1(int* b)
{
f1<N-1>(b);
}
// specialization
template<> inline void f1<0>(int*) {}
int main()
{
int a[]={1, 2};
f1<2>(a);
}
-----------------------------------------------------------------------
But I need more general:
---f2.cpp--------------------------------------------------------------
template <class Ran, int N>
inline void f2(int* b)
{
f2<Ran, N-1>(b);
}
// specialization
template<class Ran> inline void f2<Ran, 0>(int*) {}
int main()
{
int a[]={1, 2};
f2<int*,2>(a);
}
-----------------------------------------------------------------------
Am I allowed to do things this way? What does the standard say?
BTW I can't compile f2.cpp,
gcc 2.95.2:
f2.cpp:8: template-id `f2<Ran, 0>' in declaration of primary template
f2.cpp: In function `void f2<int *, -14>(int *)':
f2.cpp:4: template instantiation depth exceeds maximum of 17
f2.cpp:4: (use -ftemplate-depth-NN to increase the maximum)
f2.cpp:4: instantiating `f2<int *, -15>(int *)'
f2.cpp:4: instantiated from `f2<int *, -14>(int *)'
f2.cpp:4: instantiated from `f2<int *, -13>(int *)'
f2.cpp:4: instantiated from `f2<int *, -12>(int *)'
[ snip ]
bcc32 5.5:
Error E2141 f2.cpp 8: Declaration syntax error
Error E2413 f2.cpp 8: Invalid template declaration
[ the infinite loop skipped ;) ]
--
With all respect, Sergey. http://cpp3.virtualave.net/
mailto : ders at skeptik.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Gabriel Dos Reis <gdr@merlin.codesourcery.com>
Date: 2000/07/27 Raw View
"Sergey P. Derevyago" <ders.NOS@skeptik.net> writes:
[...]
| But I need more general:
| ---f2.cpp--------------------------------------------------------------
| template <class Ran, int N>
| inline void f2(int* b)
| {
| f2<Ran, N-1>(b);
| }
|
| // specialization
| template<class Ran> inline void f2<Ran, 0>(int*) {}
|
| int main()
| {
| int a[]={1, 2};
| f2<int*,2>(a);
| }
| -----------------------------------------------------------------------
|
| Am I allowed to do things this way? What does the standard say?
There's no such things as "function template partial specialization".
Instead, you might to use an entity that can easily be partially
specialized: a class template.
template<typename Ran, int N>
struct f_helper {
static do_it(int*) { /* ... */ }
};
template<typename Ran>
struct f_helper<Ran, 0> { /* ... */ };
template<typename Ran, int N>
void f(int* b) { f_helper<Ran, N>::do_it(b); }
--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/07/27 Raw View
[...]
> template <class Ran, int N>
> inline void f2(int* b)
> {
> f2<Ran, N-1>(b);
> }
>
> // specialization
> template<class Ran> inline void f2<Ran, 0>(int*) {}
I am afraid there is no such thing as a function template partial
specialization: the thing your code is looked like. 14.5.5.1/7 says that
your code is ill-formed. You might wish to have a look at "Specialization of
function templates" thread in comp.lang.c++ (11 May 2000). I have given the
workaround that uses functors there.
With regards,
Michael Kochetkov.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Sergey P. Derevyago" <ders.NOS@skeptik.net>
Date: 2000/07/28 Raw View
Michael Kochetkov wrote:
> [...]
> > template <class Ran, int N>
> > inline void f2(int* b)
> > {
> > f2<Ran, N-1>(b);
> > }
> >
> > // specialization
> > template<class Ran> inline void f2<Ran, 0>(int*) {}
> I am afraid there is no such thing as a function template partial
> specialization:
Why? Are there any problems with this quite usefull feature?
--
With all respect, Sergey. http://cpp3.virtualave.net/
mailto : ders at skeptik.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jerry Coffin <jcoffin@taeus.com>
Date: 2000/07/28 Raw View
In article <398002F4.6BA72F38@skeptik.net>, ders.NOS@skeptik.net
says...
[ ... ]
> > I am afraid there is no such thing as a function template partial
> > specialization:
>
> Why? Are there any problems with this quite usefull feature?
This feature has never existed so it's hard to say whether it would
be useful or not if it did. With function templates, there's partial
ordering, but not partial specialization.
In fairness, partial ordering of function templates accomplishes many
of the same sorts of things as partial specialization of class
templates, so some confusion is understandable. I don't think the
standard could easily have used the same termin for both though:
partial specialization applies only to class templates, but partial
ordering applies to both class and function templates -- partial
ordering of class and function templates are covered in 14.5.4.2 and
14.5.5.2 respectively. Note that in the case of class templates,
partial ordering applies to two partial specializations of the class,
so you've got to read carefully to keep track of which "partial X" is
being talked about at a given moment...
--
Later,
Jerry.
The universe is a figment of its own imagination.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Sergey P. Derevyago" <ders.NOS@skeptik.net>
Date: 2000/07/28 Raw View
Jerry Coffin wrote:
> > > I am afraid there is no such thing as a function template partial
> > > specialization:
> > Why? Are there any problems with this quite usefull feature?
> This feature has never existed so it's hard to say whether it would
> be useful or not if it did.
Do you really think so? :)
I need this feature in my real code. Now I have to use a wrapper class with
the static member function.
--
With all respect, Sergey. http://cpp3.virtualave.net/
mailto : ders at skeptik.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Gabriel Dos Reis <gdr@merlin.codesourcery.com>
Date: 2000/07/28 Raw View
"Sergey P. Derevyago" <ders.NOS@skeptik.net> writes:
| Jerry Coffin wrote:
| > > > I am afraid there is no such thing as a function template partial
| > > > specialization:
| > > Why? Are there any problems with this quite usefull feature?
| > This feature has never existed so it's hard to say whether it would
| > be useful or not if it did.
| Do you really think so? :)
|
| I need this feature in my real code.
Given my paranoid nature, I tend to be very suspicious when someone
starts claiming I need this or that in my code...
| ... Now I have to use a wrapper class with
| the static member function.
Which lets you get what you want. No?
Please, don't get me wrong. I'm not saying that feature wouldn't be
very nice, nor cool nor whatever. We've already a complicated
language to deal with. Let's explore and understand how to use its
already powerful features.
--
Gabriel Dos Reis, gdr@codesourcery.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/07/28 Raw View
"Sergey P. Derevyago" wrote:
>
> Jerry Coffin wrote:
> > > > I am afraid there is no such thing as a function template partial
> > > > specialization:
> > > Why? Are there any problems with this quite usefull feature?
> > This feature has never existed so it's hard to say whether it would
> > be useful or not if it did.
> Do you really think so? :)
>
> I need this feature in my real code. Now I have to use a wrapper class with
> the static member function.
You don't need partial specialization of function templates, because
you can get the same effect with overloading.
template<class A, class B> B foo(A, B);
template<class A> int foo(A, int);
template<class A, class B*> B foo(A, B*);
Classes can be partially specialized because you can't overload type names.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/07/29 Raw View
Steve Clamage wrote:
....
> You don't need partial specialization of function templates, because
> you can get the same effect with overloading.
>
> template<class A, class B> B foo(A, B);
> template<class A> int foo(A, int);
> template<class A, class B*> B foo(A, B*);
However, you can't overload template functions based on non-type
template parameters, which is precisely the problem that came up on this
thread.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]