Article View: comp.std.c++
Article #1096Recursive templates
From: Jamshid Afshar
Date: Mon, 16 Nov 1992 07:16
Date: Mon, 16 Nov 1992 07:16
52 lines
1860 bytes
1860 bytes
Has ANSI decided anything about the expansion of a recursive template
definition? Consider the following class template:
template<unsigned N>
class A {
int d[N];
public:
A<N-1> operator--() const
{ return A<N-1>(); }
A<N+1> operator++() const
{ return A<N+1>(); }
int& operator[](unsigned i)
{ return d[i]; }
};
I agree
A<0> a0;
should be an error since it results in a 0 length array, but what about
A<1> a1;
if operator--() is never called on it? Actually, will the effects of
even compiling code like this be defined, or could a compiler give an
"out of memory" error or crash (btw, BC++ 3.1 gives an error)? Would
recursion-stopping specializations for A<1> and, say, A<8> make a
difference. If so would they have to be in any particular order?
class A<1> {
public:
int d[1];
public:
A<2> operator++() const
{ return A<2>(); }
int& operator[](unsigned i)
{ return d[i]; }
};
class A<8> {
int d[8];
public:
A<7> operator--() const
{ return A<7>(); }
int& operator[](unsigned i)
{ return d[i]; }
};
I'm hoping ANSI will allow recursive templates. This would require
that compilers not expand a template function or member function
unless it is actually called. I'm worried, though, that there are
situations where this is difficult or even impossible. Has anyone
thought this through?
Thanks, Jamshid Afshar
jamshid@emx.utexas.edu
Technical Details
Message-ID:
<83733@ut-emx.uucp>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!ut-emx!emx.cc.utexas.eduShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!ut-emx!emx.cc.utexas.edu From: jamshid@emx.cc.utexas.edu (Jamshid Afshar) Newsgroups: comp.std.c++ Subject: Recursive templates Message-ID: <83733@ut-emx.uucp> Date: 16 Nov 92 07:16:12 GMT Sender: jamshid@ut-emx.uucp Organization: The University of Texas at Austin; Austin, Texas Lines: 52