Special cases in C++ program

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 
struct Factorial
{
    enum { value = N * Factorial::value };
};

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

template 
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.





你可能感兴趣的:(C++,language)