Thread View: comp.std.c++
7 messages
7 total messages
Started by Jamshid Afshar
Mon, 16 Nov 1992 07:16
Recursive templates
Author: 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
Re: Recursive templates
Author: Pete Becker
Date: Mon, 16 Nov 1992 22:05
Date: Mon, 16 Nov 1992 22:05
29 lines
1044 bytes
1044 bytes
In article <83733@ut-emx.uucp> jamshid@emx.cc.utexas.edu (Jamshid Afshar) writes:
>Has ANSI decided anything about the expansion of a recursive template
>definition?
template <unsigned n> class Factorial
{
public:
unsigned eval() { return n*Factorial<n-1>.eval(); }
};
class Factorial<0>
{
public:
unsigned eval() { return 1; }
};
#include <iostream.h>
int main()
{
Factorial<6> f6;
cout << f6.eval() << endl;
return 0;
}
That's from memory, so I may have some of the details wrong, but it
worked in BC 3.0, and from discussions I've had the same sort of thing works
with cfront.
-- Pete
Re: Recursive templates
Author: Paul Jay Lucas
Date: Tue, 17 Nov 1992 00:18
Date: Tue, 17 Nov 1992 00:18
33 lines
1372 bytes
1372 bytes
In <1992Nov16.220526.21566@borland.com> pete@borland.com (Pete Becker) writes:
>In article <83733@ut-emx.uucp> jamshid@emx.cc.utexas.edu (Jamshid Afshar) writes:
>>Has ANSI decided anything about the expansion of a recursive template
>>definition?
> template <unsigned n> class Factorial
> {
> public:
> unsigned eval() { return n*Factorial<n-1>.eval(); }
> };
> class Factorial<0>
> {
> public:
> unsigned eval() { return 1; }
> };
> #include <iostream.h>
> int main()
> {
> Factorial<6> f6;
> cout << f6.eval() << endl;
> return 0;
> }
*****> Has anyone got a *real* justification for this? This is a very
obfuscated way of writing factorial.
--
- Paul J. Lucas University of Illinois
AT&T Bell Laboratories at Urbana-Champaign
Naperville, IL pjl@cs.uiuc.edu
Re: Recursive templates
Author: Bjarne Stroustrup
Date: Tue, 17 Nov 1992 03:21
Date: Tue, 17 Nov 1992 03:21
13 lines
916 bytes
916 bytes
Jamshid Afshar jamshid@emx.utexas.edu writes: > This would require > that compilers not expand a template function or member function > unless it is actually called. This was discussed briefly in the ANSI/ISO committee and there seemed to be consensus that a template member function should not be generated unless it was used. This is therefore the most likely outcome as a more precise specification is worked out, but it is not the only opinion that has ever been voiced on this matter.
Re: Recursive templates
Author: Hong-Hsiang Chen
Date: Tue, 17 Nov 1992 15:11
Date: Tue, 17 Nov 1992 15:11
60 lines
2384 bytes
2384 bytes
In article <1992Nov16.220526.21566@borland.com> pete@borland.com (Pete Becker) writes:
>In article <83733@ut-emx.uucp> jamshid@emx.cc.utexas.edu (Jamshid Afshar) writes:
>>Has ANSI decided anything about the expansion of a recursive template
>>definition?
>
Interesting...I tried running the following example with gcc 2.3.1...
> template <unsigned n> class Factorial
> {
> public:
> unsigned eval() { return n*Factorial<n-1>.eval(); }
^^^^^^^^^^^^^^^^^^^^^
what?? there's no instance here!
I used:
static unsigned eval() { return n*(Factorial<n-1>::eval()); }
> };
>
> class Factorial<0>
> {
> public:
> unsigned eval() { return 1; }
again, I used
static unsigned eval() { return 1; }
> };
>
> #include <iostream.h>
>
> int main()
> {
> Factorial<6> f6;
> cout << f6.eval() << endl;
I used
cout << (Factorial<6>::eval()) << endl;
> return 0;
> }
>
> That's from memory, so I may have some of the details wrong, but it
>worked in BC 3.0, and from discussions I've had the same sort of thing works
>with cfront.
Well, it seems that it would make more sense to have no instantations of the
class at all...
> -- Pete
Interesting that it actually works with gcc 2.3.1...impressive...
Come to think of it, there shouldn't be any reason why recursive templates
shouldn't work...anybody have an argument against it? (besides it being a
vehicle for more obsfucated code :-))
Curiously, gcc 2.3.1 gives me a warning about shadowing member 'eval' with
member function... I guess I should use virtuals, eh?
Well, more thoughts on this later...
--
-----
Sean Chen. hhc@athena.mit.edu
"Nunc in scutella iaceo,/et volitare nequeo,/dentes fredentes video./Miser!"
Olim lacus colueram, _Carmina Burana_
Re: Recursive templates
Author: Pete Becker
Date: Tue, 17 Nov 1992 17:21
Date: Tue, 17 Nov 1992 17:21
12 lines
1159 bytes
1159 bytes
In article <Bxu3J4.F93@news.cso.uiuc.edu> pl2440@meibm31.cen.uiuc.edu (Paul Jay Lucas) writes: > >*****> Has anyone got a *real* justification for this? This is a very > obfuscated way of writing factorial. Has anyone got a *real* justification for recursive functions? They just provide a very obfuscated way of writing factorial(). The point of my original posting was simply that there is no work to be done to specify how such things work, because they already work in exactly the way you'd expect. Whether there are useful things that can be done with such constructs is left as an exercise for the reader... -- Pete
Re: Recursive templates
Author: Paul Lucas
Date: Tue, 17 Nov 1992 22:53
Date: Tue, 17 Nov 1992 22:53
30 lines
1762 bytes
1762 bytes
In <1992Nov17.172152.14255@borland.com> pete@borland.com (Pete Becker) writes: >In article <Bxu3J4.F93@news.cso.uiuc.edu> pl2440@meibm31.cen.uiuc.edu (Paul Jay Lucas) writes: >> >>*****> Has anyone got a *real* justification for this? This is a very >> obfuscated way of writing factorial. > Has anyone got a *real* justification for recursive functions? They >just provide a very obfuscated way of writing factorial(). *****> Some algorithms are naturally recursive; most (other) people would agree that factorial is naturally expressed recursively. > The point of my original posting was simply that there is no work to >be done to specify how such things work, because they already work in exactly >the way you'd expect. *****> Huh? I don't understand that paragraph. >Whether there are useful things that can be done with >such constructs is left as an exercise for the reader... *****> No, the point is whether it should be mandated that recursive templates be part of the standard or not. If they're trivial to implement in compilers, then go ahead; if not, then I think justification is in order. -- - Paul J. Lucas University of Illinois AT&T Bell Laboratories at Urbana-Champaign Naperville, IL pjl@cs.uiuc.edu
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads