template metaprogramming

Here is an example of metaprogramming in C++:


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

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

void fact()
{
    int x = Factorial<4>::value; // == 24
    int y = Factorial<0>::value; // == 1
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章