From: Alex on
{ edits: 22 excess spaces in the commented code line removed.

The question of this article is in the subject line
"VS 2005 compiler died on this recursive template".
-mod }


template <int i>
struct Factorial
{
enum { N = i<=0? 1 : i*Factorial<i-1>::N };
};
//template<> struct Factorial<0> { enum { N = 1 }; };
// IF uncoment � working fine
void main() {
int factorial=Factorial<3>::N;
}



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 16 Mrz., 05:25, Alex <alexey.d.zait...(a)gmail.com> wrote:
> template <int i>
> struct Factorial
> {
> enum { N = i<=0? 1 : i*Factorial<i-1>::N };};
>
> //template<> struct Factorial<0> { enum { N = 1 }; };
> // IF uncoment � working fine
> void main() {
> int factorial=Factorial<3>::N;
> }

1) The main function must have an int return type.

2) The specialization is required. It doesn't help that
you check "i <= 0" inside the primary template to
prevent the compiler from instantiating Factorial<i-1>
because the kind of expression it occurs in, naming
a member via qualification ("Factorial<i-1>::N"), does
require an instantiation of the class Factorial<i-1>
even though the branch will not be entered.

But *if* it instantiates Factorial<i-1>, this is a never-
ending story without any explicit specialization.

HTH & Greetings from Bremen,

- Daniel


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Bart van Ingen Schenau on
On Mar 16, 5:25 am, Alex <alexey.d.zait...(a)gmail.com> wrote:
> { edits: 22 excess spaces in the commented code line removed.
>
> The question of this article is in the subject line
> "VS 2005 compiler died on this recursive template".
> -mod }
>
> template <int i>
> struct Factorial
> {
> enum { N = i<=0? 1 : i*Factorial<i-1>::N };};

The test in the ?:-operator does not break the recursive instantiation
of the template, because the rightmost argument still has to be
instantiated (even if i is 0 or less) to have a valid expression.
Only after it is verified that the expression is valid can the test
come into play, but that is already too late.

>
> //template<> struct Factorial<0> { enum { N = 1 }; };
> // IF uncoment � working fine
> void main() {
> int factorial=Factorial<3>::N;
>
> }
>

Bart v Ingen Schenau


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]