模板元编程

其实一个重要思想就是利用局部特化。注意特化不仅仅可以用类型,还可以用数值。如下:

// 主模板
template<int N>
struct Fib
{
    enum { Result = Fib<N-1>::Result + Fib<N-2>::Result };
};

// 完全特化版
template <>
struct Fib<1>
{
    enum { Result = 1 };
};


// 完全特化版
template <>
struct Fib<0>
{
    enum { Result = 0 };
};

int main()
{
    int i = Fib<10>::Result;
    // std::cout << i << std::endl;
}
原文地址:https://www.cnblogs.com/charlesblc/p/6483287.html