|
From: Ioannis V. <no...@ho...> - 2003-02-06 12:08:14
|
I discussed templates with some other coder and here is a cool thing that=
we
discussed. A nice way of how to make calculations at compile time instead=
of
run-time. This method uses uses the template ability to pass *constant*
variables as template arguments:=0D
=0D
=0D
Check this out:=0D
=0D
#include <iostream>=0D
=0D
template<int N>=0D
struct factorial=0D
{=0D
static const int value =3D N*factorial<N-1>::value;=0D
};=0D
=0D
template<>=0D
struct factorial<0>=0D
{=0D
static const int value =3D 1;=0D
};=0D
=0D
=0D
int main()=0D
{=0D
std::cout<<factorial<5>::value<<std::endl;=0D
=0D
}=0D
=0D
=0D
I like this thing very much. We can reduce runtime space(recursions) /
time(recursions,iterations) costs elegantly (but increasing compile time)=
=2E=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20
|