Redian新闻
>
C++里get array size的问题 (转载)
avatar
L*s
2
新买了英格兰足球队球鞋, 怎样把它加在形象上。
avatar
Y*e
3
暖色调容易给人温暖奔放被接受的感觉;冷色调则相反。
而我最推崇湖绿湖蓝。
原因引如下摘录:
“在艺术观点上,印象主义画家反对当时占正统地位的古典学院派,反对日益落入俗套
、矫揉造作的浪漫主义绘画(Romanticism),而是在C.柯罗、巴比松画派和G.库尔贝等
人的写实画风的推动下,吸收荷兰、英国、西班牙、日本、中国等国家绘画的营养,同
时受现代科学,尤其是光学的启发,认为一切色彩皆产生于光,于是他们依据光谱赤橙
黄绿青蓝紫七色来调配颜色。由于光是瞬息万变的,他们认为只有捕捉瞬息间光的照耀
才能揭示自然界的奥妙。因此在绘画中注重对外光的研究和表现,主张到户外去,在阳
光下依据眼睛的观察和现场的直感作画,表现物象在光的照射下,色彩的微妙变化。由
此印象主义绘画在阴影的处理上,一反传统绘画的黑色而改用有亮度的青、紫等色。印
象派绘画用点取代了传统绘画简单的线与面,从而达到传统绘画所无法达到的对光的描
绘。具体的说,当我们从近处观察印象派绘画作品时,我们看到的是许多不同的色彩凌
乱的点,但是当我们从远处观察他们时,这些点就会像七色光一样汇聚起来,给人光的
感觉,达到异想不到的效果。”
avatar
h*g
4
【 以下文字转载自 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吗? 我试试了,好象是可以的。中间有什么错
误吗?
avatar
q*9
5
你是求职啊还是招聘呢
avatar
c*y
6
个人家页——我的物品——选择鞋子——保存形象

【在 L****s 的大作中提到】
: 新买了英格兰足球队球鞋, 怎样把它加在形象上。
avatar
d*p
7
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.
avatar
L*s
8
多谢。
看来我老了,不中用了。
avatar
p*o
9
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.

avatar
E*V
10
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?

avatar
d*p
11
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
avatar
d*p
12
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

avatar
p*o
13
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

avatar
m*s
14

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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。