h*****g 发帖数: 944 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: huasing (Menlo Park), 信区: JobHunting
标 题: C++里get array size的问题
发信站: BBS 未名空间站 (Wed Jul 21 13:45:52 2010, 美东)
C++里是不是现在没有现成的get array size/length的function??
如果没有的话,我在网上看到了一个code
#include
template char (&array(T(&)[N]))[N];
#define length(a) (sizeof a / sizeof a[0])
int main()
{
int a[10];
std::cout << sizeof array(a) << '\n';
std::cout << length(a) << '\n';
}
请问这个code对所有的data type都work吗? 我试试了,好象是可以的。中间有什么错
误吗? |
d****p 发帖数: 685 | 2 Interesting. Should be working in theory.
However, it is just a syntax sugar and I doubt if all compilers support it
well since it makes the parsing complicate. |
p***o 发帖数: 1252 | 3 Most likely they won't work under the situation LZ needs them to work,
e.g. after passing the array to a function ...
BTW, I don't see the advantage of the template approach compared to
the sizeof/sizeof approach. Is it more robust?
【在 d****p 的大作中提到】 : Interesting. Should be working in theory. : However, it is just a syntax sugar and I doubt if all compilers support it : well since it makes the parsing complicate.
|
E*V 发帖数: 17544 | 4 the template one is computed at compile time?
【在 p***o 的大作中提到】 : Most likely they won't work under the situation LZ needs them to work, : e.g. after passing the array to a function ... : BTW, I don't see the advantage of the template approach compared to : the sizeof/sizeof approach. Is it more robust?
|
d****p 发帖数: 685 | 5 Yes, I agree with you that sizeof(array)/sizeof(array_elem) is better.
Lz's code however technically is right - declare a function which takes in a
pointer to an array whose length is known at compile time and returns a
pointer to an array whose length is also known.
A pointer to an array is slightly different with an array from the
perspective of compiler though the sizeof operator will produce the same
result for them.
Basically you cannot return an array from a function but you can return a
p |
d****p 发帖数: 685 | 6 Took another look.
The error message from Solaris CC is against
template char (&array(T(&)[N]))[N];
The last [N] was treated a subscript operator and N was expected to be
constant.
If typedef template is supported in C++, we can get this around by
template
typedef T(&T_N_array)[N];
template T_N_array array(T(&)[N]);
a
or
【在 d****p 的大作中提到】 : Yes, I agree with you that sizeof(array)/sizeof(array_elem) is better. : Lz's code however technically is right - declare a function which takes in a : pointer to an array whose length is known at compile time and returns a : pointer to an array whose length is also known. : A pointer to an array is slightly different with an array from the : perspective of compiler though the sizeof operator will produce the same : result for them. : Basically you cannot return an array from a function but you can return a : p
|
p***o 发帖数: 1252 | 7 Beginners usually write code like follows ;)
void func(int array[N])
{
int size = sizeof(array)/sizeof(array[0]);
...
}
a
or
【在 d****p 的大作中提到】 : Yes, I agree with you that sizeof(array)/sizeof(array_elem) is better. : Lz's code however technically is right - declare a function which takes in a : pointer to an array whose length is known at compile time and returns a : pointer to an array whose length is also known. : A pointer to an array is slightly different with an array from the : perspective of compiler though the sizeof operator will produce the same : result for them. : Basically you cannot return an array from a function but you can return a : p
|
m*****s 发帖数: 2 | 8
a
The template function takes in a "reference" to an array, instead of a "
pointer"
to an array and so is the return value.
The advantage for the template version is that it enforces the compile time
type
check that the passed in parameter be an array of T, not a pointer to T,
which
is better than the second version that accepts a pointer to T (or any object
that
overloads [], like a vector) and generates misleading result.
or
【在 d****p 的大作中提到】 : Yes, I agree with you that sizeof(array)/sizeof(array_elem) is better. : Lz's code however technically is right - declare a function which takes in a : pointer to an array whose length is known at compile time and returns a : pointer to an array whose length is also known. : A pointer to an array is slightly different with an array from the : perspective of compiler though the sizeof operator will produce the same : result for them. : Basically you cannot return an array from a function but you can return a : p
|