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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值