由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++里get array size的问题 (转载)
相关主题
私有成员不能用类成员函数修改?用数组做参数,在函数内部如何知道数组的size?
a simple question for C++ classc++ template question:
C++ 菜鸟问一个关于template 的问题。一个partial specialization的问题
boost::function 的 syntax 问题C++ question about template typedef
一个关于C++ template和overload的问题如何 define/initialize static data member of a class templ
腆着脸在问一道Help: who has gcc 4.0 or higher
C++ Q04: template memberpointer to function
C++ linking 弱问 (one file)一个指向指针的指针的引用?
相关话题的讨论汇总
话题: array话题: sizeof话题: template话题: c++话题: int
1 (共1页)
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

1 (共1页)
相关主题
一个指向指针的指针的引用?一个关于C++ template和overload的问题
a question about bitwise operation腆着脸在问一道
c++ template question:C++ Q04: template member
请问const myClass &src 和myClass const &src有什么区别?C++ linking 弱问 (one file)
私有成员不能用类成员函数修改?用数组做参数,在函数内部如何知道数组的size?
a simple question for C++ classc++ template question:
C++ 菜鸟问一个关于template 的问题。一个partial specialization的问题
boost::function 的 syntax 问题C++ question about template typedef
相关话题的讨论汇总
话题: array话题: sizeof话题: template话题: c++话题: int