VS 2005 compiler died on template (part I)

A

Alex

template <int i> struct Factorial {
enum { N = i<=0? 1 : i*Factorial<i-1>::N };
};
template <> struct Factorial <-500> { enum { N = 1 }; };
const int factorial=Factorial<5>::N;
 
M

Michael Doubez

template <int i> struct Factorial {
        enum { N = i<=0? 1 : i*Factorial<i-1>::N  };};

template <>       struct Factorial <-500>   { enum { N = 1 }; };
const int factorial=Factorial<5>::N;

template <int i, bool positive = i>0 > struct FactorialImpl;

template <int i>
struct FactorialImpl<i,true>
{
enum { N = i*Factorial<i-1>::N };
};

template <int i>
struct FactorialImpl<i,false>
{
enum { N = 1 };
};

template <int i>
struct Factorial
{
enum { N = FactorialImpl<i>::N };
};
 
V

Vladimir Jovic

Alex said:
template <int i> struct Factorial {
enum { N = i<=0? 1 : i*Factorial<i-1>::N };
};
template <> struct Factorial <-500> { enum { N = 1 }; };
const int factorial=Factorial<5>::N;


The factorial of a negative number is not defined, therefore your
template should be:

template < unsigned int i >
struct Factorial
{
enum { N = i * Factorial< i-1 >::N };
};

template<>
struct Factorial< 0 >
{
enum { N = 1 };
};
 
A

Alex

template <int i, bool positive = i>0 > struct FactorialImpl;

template <int i>
struct FactorialImpl<i,true>
{
  enum { N = i*Factorial<i-1>::N };

};

template <int i>
struct FactorialImpl<i,false>
{
  enum { N = 1 };

};

template <int i>
struct Factorial
{
  enum { N = FactorialImpl<i>::N };

};

Thanks, Michael
I just want to say that this code caused
abnormally termination of VS 2005 compiler
in the case of <-500>. with <0> - working fine.
 
M

Michael Doubez

I just want to say that this code caused
abnormally termination of VS 2005 compiler
in the case of <-500>. with <0> - working fine.

It just proves that for your compiler (perhaps with you options), with
i==0, the line
N = i<=0? 1 : i*Factorial<i-1>::N

doesn't instantiate Factorial<i-1>.

And ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,771
Messages
2,569,587
Members
45,099
Latest member
AmbrosePri
Top