Redian新闻
>
template metaprogramming 的问题
avatar
template metaprogramming 的问题# Programming - 葵花宝典
h*e
1
大家都知道STL里面有个valarray,definition是
template
class valarray
{
...
}
有人说有个更efficient的definition就是
template
class valarray
{
static const unsigned int count = N;
...
}
说因为array size在compile time就知道了,所以可以使用例如
valarray a = { 1, 2, 3 };
我比较confused的是,如果程序中都是valarry这类的东东,是比
valarray a(3);要efficient。但是code如果都是象
void func(const unsigned int size)
{
valarray a;
...
}
这样的,有何efficiency可言呢?这只是一个小例子来反映我
对template metaprogramming的很
avatar
t*t
2
first thing you need to know:
(non-type, non-template> template parameter must be either (integral)
constant, or some object with external linkage. In other words, it must be
either constant value, or has constant address, at least (string literals
are exception, they are not allowed). So it is not possible to have a
template with auto variable as parameter.
second, std::valarray does not allow size parameter.
avatar
p*u
3

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~he said he's got a different
implementation of valarray, which is
template class valarray.
Why doesn't std::valarray allow size parameter?

【在 t****t 的大作中提到】
: first thing you need to know:
: (non-type, non-template> template parameter must be either (integral)
: constant, or some object with external linkage. In other words, it must be
: either constant value, or has constant address, at least (string literals
: are exception, they are not allowed). So it is not possible to have a
: template with auto variable as parameter.
: second, std::valarray does not allow size parameter.

avatar
t*t
4

en....because the standard committee doesn't like it?

【在 p*u 的大作中提到】
:
: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~he said he's got a different
: implementation of valarray, which is
: template class valarray.
: Why doesn't std::valarray allow size parameter?

avatar
h*e
5
不好意思我的语法错了,但是我的问题是,如果array size能够
在compile time就知道,那么我们完全可以用
static std::valarray a(3) = {1, 2, 3};
为什么说valarray a = {1, 2, 3}更efficient呢??

【在 t****t 的大作中提到】
:
: en....because the standard committee doesn't like it?

avatar
c*e
6
first of all, it would be nice if you can provide us with the source of the
words you heard of
as to the question itself, cerntainly there could be optimization
that could be done if u have vararry
for example, in that case you can use a real array in the implementation
of the vararry and dont have to worry about if you must grow memory
for the newly inserted element when it is about to get out of boundary etc.

【在 h****e 的大作中提到】
: 不好意思我的语法错了,但是我的问题是,如果array size能够
: 在compile time就知道,那么我们完全可以用
: static std::valarray a(3) = {1, 2, 3};
: 为什么说valarray a = {1, 2, 3}更efficient呢??

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