模板元编程中的“hello world”

摘自 effective c++

// TODO 条款48 Be aware of template metaprogramming. //  [9/29/2013 xiarl]

template<unsigned n>
struct Factorial
{
    enum
    {
        value = n* Factorial<n-1>::value
    };
};

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

int _tmain(int argc, TCHAR * argv[])
{

    int ivalue = Factorial<9>::value;//362880

    return 0;
}

posted @ 2013-09-29 14:18  xiarunliang  阅读(154)  评论(0编辑  收藏  举报