Topic: template function specialization (fixed)


Author: "Sergey P. Derevyago" <ders.NOS@skeptik.net>
Date: 2000/07/27
Raw View
There was a typo in my previous post, sorry. Here is the fixed version:

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(Ran b)
{
 f2<Ran, N-1>(b);
}

// specialization
template<class Ran> inline void f2<Ran, 0>(Ran) {}

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              ]