Special cases in C++ program

来源:互联网 发布:m4螺纹编程 编辑:程序博客网 时间:2024/05/16 15:14

Recently, I have accepted the challenge to read code in a github repository called mshadow. But only from the language grammar. I found that I could not understand template<> in the code.

So what I do is to search it from internet. And this is what I found. Try to understand the following codes.

1. Template <>

template <int N>struct Factorial{    enum { value = N * Factorial<N - 1>::value };};//reconstrunction???template <>struct Factorial<0>{    enum { value = 1 };};template <int N>struct Test{    int value = N+1;};template<>struct Test<0>{    int a = 0+2;};

The first structure is Factorial, but since it is recursive, so we need to define the seed-- from where to start. We use
template <>struct Factorial<0>
It means that there only has one structure, if N is not 0, use the first definition of this structure, if it is 0, use the second part of this structure.

This way, it is easy to understand the second template called Test here.

2. To be continued.





0 0
原创粉丝点击