C++利用模板函数得到数组长度

版权声明:未经博主允许请勿转载 https://blog.csdn.net/hlz_12345/article/details/83901308
#include <iostream>
using namespace std;
template <int N>
void arrlength1(const char(&a)[N]) //利用模板函数得到数组长度
{
	cout << sizeof(a) << endl;
}
void arrlength2(const char *a)  //数组首地址进入函数后就只能得到指针大小了
{
	cout << sizeof(a) << endl;
}
int main()
{
    char ch[] = "hello,world";
    arrlength1(ch);
    arrlength2(ch);
    system("pause");
    return 0;  
} 

 

 

猜你喜欢

转载自blog.csdn.net/hlz_12345/article/details/83901308