模板与const

int compare(const T& a, const T& b)  { //普通函数大括号前不能加const
    if (a > b) return 1;
    if (a < b) return -1;
    return 0;
}

//大括号前,参数列表之后的const只用于类的非static成员函数,不能用于其他函数
class A {
public:
    void func()const {} //const表示,this指针是const A* const this; this指针是const,this所指也是const
    void func() {} //与上面是重载函数,是两个函数,因为形参不同,this指针A* const this; this指针是const,this所指可以修改
};

template<unsigned N,unsigned M>
int compare(char(&p1)[N], char(&p2)[M]) {
    return strcmp(p1, p2);
}

//template<typename T> inline  //注意inline的位置

//接受数组实参的begin与end,[注] 返回的是一个指针
template<typename T,unsigned N>
T* begin(const T (&arr)[N]) {
    return arr;
}
template<typename T,unsigned N>
T* end(const T(&arr)[N]) {
    return arr + N;
}

//constexpr模板,返回给定数组大小
template<typename T,unsigned N>
unsigned sizes(const T(&arr)[N]) {
    return N;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值