通过const&传递参数......可以过头了吗? [英] passing parameters by const& ... can it be overdone?

查看:76
本文介绍了通过const&传递参数......可以过头了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说将函数参数传递给

const&这是真的吗?是否有可能过火?

如果是这样,为什么?非常感谢大家!


克里斯托弗迪金斯
http://www.cdiggins.com

I have heard it is considered good practice to pass function parameters as
const& as often as possible, is this true? Is it possible to go overboard?
And if so why? Thanks a lot in advance everyone!

Christopher Diggins
http://www.cdiggins.com

推荐答案

christopher diggins写道:
christopher diggins wrote:
我听说过将函数参数传递为
const& amp;这是真的吗?是否有可能过火?
如果是这样,为什么?非常感谢大家!
I have heard it is considered good practice to pass function parameters as
const& as often as possible, is this true? Is it possible to go overboard?
And if so why? Thanks a lot in advance everyone!




当然......传递不是很大的东西是没有意义的。


a复制很可能会更快。


然而,当涉及到时,通过const引用开始(指针,整数,双精......等)复制是非常重要的事情:向量,

字符串等......你知道它不会在调用者中改变,

然后那里'没有复制它是一个优点。



Certainly...it makes little sense to pass things that aren''t very big
to begin with (pointers, ints, doubles, etc...) via const reference when
a copy would most likely be faster.

However when it comes to things where the copying is nontrivial: vectors,
strings, etc...and you know that it''s not going to change in the caller,
then there''s an advantage to not copying it.


" christopher diggins" < CD ****** @ videotron.ca>在留言新闻中写道:3k ********************* @ weber.videotron.net ..
"christopher diggins" <cd******@videotron.ca> wrote in message news:3k*********************@weber.videotron.net.. .
我听说它被认为是将函数参数作为常量传递的良好实践这是真的吗?是否有可能过火?


据我所知,唯一值得一试的经验值是

''不要依赖于经验法则而不知道何时何打破他们。''"。


你建议的规则不值得一试。因此应用它

很可能会导致过度使用。

如果是这样,为什么?非常感谢大家!


对于复制费用昂贵的对象,包括大型对象

或复制结构必须分配动态内存的对象,

通过const引用传递将是个好主意,除非有锁定问题甚至会使复制成为更便宜的选择。

当考虑通过避免复制获得的收益时,一个必须

考虑额外的间接参考通常需要每次访问成员或对象的价值




实现规则是下铺的最简单方法是考虑

的简单值对象(可能,在特定的机器上)

适合机器单词,例如int,float或small structs。当

这些可以用一条

指令复制到参数/调用堆栈时,使用引用可能会失败。

但即使这样也不能成为一项严格的规则。对于

的例子,这样一个小对象可能很少用在

调用的例程中,并且往往驻留在已经分页的内存中。因为传递它的地址而不是使用它会比以页面错误为代价提取价值更快。


好​​的做法是理解什么影响性能和

在设计和编码过程中牢记这一点,但最后衡量一下b * b性能,如果缺乏,则发现原因并纠正。如果

在设计中没有做出愚蠢的错误,那么在这一点上需要的修复应该主要是本地化的更改。


如果我不清楚这一点:好的做法

是在不知道其限制的情况下避免经验法则。

Christopher Diggins
http://www.cdiggins.com
I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it
possible to go overboard?
I have been known to say "The only rule of thumb worth a damn is
''Do not rely on rules of thumb without knowing when to break them.''".

The rule you suggest is not worth a damn, period. So applying it
would be quite likely to lead to overuse.
And if so why? Thanks a lot in advance everyone!
For objects that are expensive to copy, which includes large objects
or objects whose copy contructors must allocate dynamic memory,
passing via const reference would be good idea, unless there were
locking issues that would make even the copying a cheaper choice.
When considering the gain achieved by avoiding a copy, one must
consider the extra indirection that a reference will usually entail for
each access to the members or value of the object.

The easiest way to realize that the rule is bunk is to consider the
case of simple value objects that (may, on a particular machine)
fit in a machine word, such as int, float, or small structs. When
these can be copied to the parameter/call stack with a single
instruction, using a reference instead would likely be a loser.
But even that cannot be taken as a hard and fast rule. For
example, such a small object might be only rarely used in the
called routine and tend to reside in memory that has been
paged out so that passing its address and not using it would
be faster than pulling the value at the cost of a page fault.

Good practise is to understand what affects performance and
keep it in mind during design and coding, but finally measure
performance and, if lacking, discover why and correct. If
silly errors have not been made in the design, such fixes as
are needed at that point should be mostly localized changes.

And, in case I have not been clear about this: Good practise
is to avoid rules of thumb without knowing their limitations.
Christopher Diggins
http://www.cdiggins.com



-

- 拉里布拉斯菲尔德

电子邮件: DO ******** @ hotmail.com

以上观点可能只属于我。



--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.


christopher diggins写道:
christopher diggins wrote:
我听说是被认为是良好的做法将函数参数传递为const&尽可能经常。
这是真的吗?


是的。

您还可以按值传递小物件。

是否有可能过火?如果是这样,为什么?
I have heard it is considered good practice
to pass function parameters as const& as often as possible.
Is this true?
Yes.
You can also pass small objects by value.
Is it possible to go overboard? And, if so, why?




你应该传递值,const *或const&返回值。

返回void的函数很少被证明是合理的。

析构函数是这个规则的一个例外。


有时候,有必要就地修改一个大对象*。

例如:

离散有限傅里叶变换,矩阵分解和

对容器对象的其他操作。


作业:


X& X:operator =(const X&);


是一个修改其中一个参数的函数的例子 -

隐藏的参数X * this 。

修改其中一个参数的函数

应该返回一个引用或指向修改后的对象的指针

而不是void这样的函数可以在表达式中使用。


但是,通常情况下,如果你发现自己实现了一个修改其一个或多个参数的函数



你应该停下来仔细考虑你的设计。

修改的参数必须在调用程序中声明为[state]变量

这将使您和其他程序员分析您的程序变得复杂

谁必须维护您的程序。



You should pass by value, const* or const& and return by value.
Functions that return void are seldom justified.
A destructor is an example of an exception to this rule.

Sometimes, it is necessary to modify a large object *in-place*.
Examples are:
discrete finite Fourier transforms, matrix decompositions and
other operations on container objects.

Assignment:

X& X:operator=(const X&);

is an example of a function that modifies one of its arguments --
the hidden argument X* this.
Functions that modify one of their arguments
should return a reference or a pointer to the modified object
instead of void so that the function can be used in an expression.

But, generally, if you find yourself implementing a function
that modifies one or more of its arguments,
you should stop and think carefully about your design.
The argument that is modified must be declared as a [state] variable
in the calling program and this will complicate
the analysis of your program by you and other programmers
who must maintain your program.

这篇关于通过const&amp;传递参数......可以过头了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆