template calculates 4 + 3 + 2 + 1


This is a “hello world” for template metaprogramming.
MetaFunc() return 10 because (4 + 3 + 2 + 1) equals 10.

template<int n>
int MetaFunc()
{
	return n +  MetaFunc<n-1>();
}
template<>
int MetaFunc<1>()
{
	return 1;
}
int TestMetaFunc()
{
	return MetaFunc<4>(); //4 + 3 + 2 + 1 == 10
}
Advertisements
Comments are closed.
%d bloggers like this: