####
The following 2 c++ template functions can detect array's dimension at compile time. But they will fail when dealing with variable length array(introduced by C99).
template<typename T, int len >int capacity_of_1Darray( T(&array)[len] )
{
return len;
}
template<typename T, int l, int m>void capacity_of_2Darray(T (&array)[l][m], int &x, int &y)
{
x=l;
y=m;
The following 2 c++ template functions can detect array's dimension at compile time. But they will fail when dealing with variable length array(introduced by C99).
template<typename T, int len >int capacity_of_1Darray( T(&array)[len] )
{
return len;
}
template<typename T, int l, int m>void capacity_of_2Darray(T (&array)[l][m], int &x, int &y)
{
x=l;
y=m;
}
本文介绍两个C++模板函数,它们可以在编译时检测一维和二维数组的长度。需要注意的是,这些函数不适用于C99引入的变长数组。
1347

被折叠的 条评论
为什么被折叠?



