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;
}