通过const&并返回临时值与传递值和返回值 [英] Passing by const & and returning a temp vs passing by value and returningit

查看:51
本文介绍了通过const&并返回临时值与传递值和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在项目中我维护我已经看到两种不同的技术用于从函数返回一个对象的
。一个是


AType函数(AType const& arg)

{

AType retval(arg); //或默认构造然后..

//一些其他处理和/或更改''retval''

返回retval;

}


void foo()

{

AType somevariable;


AType anothervariable = function(somevariable); //通过参考

//其他一些处理

}


和另一个是

AType函数(AType retval)

{

//其他一些处理和更改''retval''

返回retval ;

}


void foo()

{

AType somevariable;


AType anothervariable = function(somevariable); //按价值传递

//其他一些处理

}


正如你所看到的差异是什么时候变量哪个稍后返回

构建。是的,我意识到差异真的很小。这可能对性能没有任何影响(我不会尝试推测一种方式或者另一种方式)。构建的对象[copy-]的数量可能是相同的(即使在理论上,RVO除外)。传递的对象是

*而不是*多态。我忘记了什么?复制构造定义为
,一般不会播放任何技巧。所有相对直接 -

前进。


现在,我的问题是,为什么我更喜欢/保留一种使用方式
$ b传递给函数的$ b值还是应该保留两者?您是否在同一项目中看到了表面下的任何珊瑚礁,一种方法的任何潜在问题,或者两种方法都有?b $ b?例如,优化器会绊倒

中的一个方法而不是其他方法吗?


我会感激任何见解。谢谢!


V

In the project I''m maintaining I''ve seen two distinct techniques used for
returning an object from a function. One is

AType function(AType const& arg)
{
AType retval(arg); // or default construction and then..
// some other processing and/or changing ''retval''
return retval;
}

void foo()
{
AType somevariable;

AType anothervariable = function(somevariable); // pass by ref
// some other processing
}

and the other is

AType function(AType retval)
{
// some other processing and changing ''retval''
return retval;
}

void foo()
{
AType somevariable;

AType anothervariable = function(somevariable); // pass by value
// some other processing
}

As you can see the difference is when the variable which later is returned
is constructed. Yes, I realize the difference is really minimal. It is
probably has no effect on performance (and I wouldn''t try to speculate one
way or another). The number of objects [copy-] constructed is probably
the same (even in theory, RVO aside). The objects being passed around are
*not* polymorphic. What am I forgetting? Copy-construction is defined
for them and in general no tricks are played. All relatively straight-
forward.

Now, my question is, why would I want to prefer/preserve one way of using
values passed to the function or should I keep both? Do you see any reefs
under the surface, any potential issues with one method or with having
both methods in the same project? Would the optimizer stumble on one of
the methods and not the other, for example?

I''d appreciate any insight. Thanks!

V

推荐答案

>>构造的对象[copy-]的数量可能是
>> The number of objects [copy-] constructed is probably
相同(即使在理论上,RVO不包括
the same (even in theory, RVO aside


No.

AType函数(AType retval) - >复制构造函数将被调用。

仅当AType具有简单快速的初始化时才使用此版本

否则通过通过引用参数更好(const引用,如果你

不会修改参数)。

Ahmed MOHAMED ALI


" Victor Bazarov"< v。******** @ comAcast.net>在留言中写道

新闻:nO **************** ***@newsread1.mlpsca01.us.t o.verio.net ...在项目中我维护我已经看到两种不同的技术用于从函数返回一个对象。一个是

AType函数(AType const& arg)
{AType retval(arg); //或默认构造然后..
//其他一些处理和/或改变' retval''
返回retval;


void foo()
{AT / / AType somevariable;

AType anothervariable =函数(somevariable); //通过参考
//其他一些处理
}



AType函数(AType retval)
{
//其他一些处理和更改''retval''
返回retval;


void foo()
{
AType somevariable;

AType anothervariable = function(somevariable); //通过值传递
//其他一些处理


正如您所看到的那样,区别在于后来返回的变量构建的时间。是的,我意识到差异真的很小。它可能对性能没有影响(我不会尝试推测一种或另一种方式)。构建的对象[copy-]的数量可能相同(即使在理论上,也不包括RVO)。传递的对象是
*不是*多态的。我忘记了什么?复制构造是为他们定义的,一般不会播放任何技巧。所有相对直接的
前进。

现在,我的问题是,为什么我更喜欢/保留一种使用传递给函数的值的方法,或者我应该保留两种方法?您是否看到地表下的任何珊瑚礁,一种方法的任何潜在问题或同一项目中的两种方法?例如,优化器会绊倒其中一种方法而不是其他方法吗?

我会很感激任何见解。谢谢!

V

No.
AType function(AType retval) --> copy constructor will be called .
Use this version only if AType has a simple and fast initialization
otherwise passing argument by reference is better ( const reference if you
will not modify the argument) .
Ahmed MOHAMED ALI

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:nO*******************@newsread1.mlpsca01.us.t o.verio.net... In the project I''m maintaining I''ve seen two distinct techniques used for
returning an object from a function. One is

AType function(AType const& arg)
{
AType retval(arg); // or default construction and then..
// some other processing and/or changing ''retval''
return retval;
}

void foo()
{
AType somevariable;

AType anothervariable = function(somevariable); // pass by ref
// some other processing
}

and the other is

AType function(AType retval)
{
// some other processing and changing ''retval''
return retval;
}

void foo()
{
AType somevariable;

AType anothervariable = function(somevariable); // pass by value
// some other processing
}

As you can see the difference is when the variable which later is returned
is constructed. Yes, I realize the difference is really minimal. It is
probably has no effect on performance (and I wouldn''t try to speculate one
way or another). The number of objects [copy-] constructed is probably
the same (even in theory, RVO aside). The objects being passed around are
*not* polymorphic. What am I forgetting? Copy-construction is defined
for them and in general no tricks are played. All relatively straight-
forward.

Now, my question is, why would I want to prefer/preserve one way of using
values passed to the function or should I keep both? Do you see any reefs
under the surface, any potential issues with one method or with having
both methods in the same project? Would the optimizer stumble on one of
the methods and not the other, for example?

I''d appreciate any insight. Thanks!

V





" Ahmed MOHAMED ALI" <啊***** @ wanadoo.fr>在消息中写道

news:42 *********************** @ news.wanadoo.fr ...

"Ahmed MOHAMED ALI" <ah*****@wanadoo.fr> wrote in message
news:42***********************@news.wanadoo.fr...
构建的对象[copy-]的数量可能相同(即使在理论上,RVO也没有。
AType函数(AType retval) - >复制构造函数将被调用。


Eh?再次查看他的帖子:

AType函数(AType const& arg)
{
AType retval(arg); //复制构造函数在这里调用
return retval;
}
The number of objects [copy-] constructed is probably
the same (even in theory, RVO aside No.
AType function(AType retval) --> copy constructor will be called .
Eh? Look at his post again:
AType function(AType const& arg)
{
AType retval(arg); // copy constructor called here
return retval;
}



复制构造函数也在那里调用。区别在于它在函数内部调用了
,而在另一种情况下调用它是为了

首先将副本传递给函数。

仅当AType具有简单快速的初始化时才使用此版本
否则通过引用传递参数更好(const参考)如果你不会修改这个论点。
Ahmed MOHAMED ALI



The copy constructor is called there, too. The difference is that here it''s
called inside the function, and in the other case it was called in order to
pass a copy to the function in the first place.
Use this version only if AType has a simple and fast initialization
otherwise passing argument by reference is better ( const reference if you
will not modify the argument) .
Ahmed MOHAMED ALI




我想你错过了我上面提到的,这两个版本都在制作

副本。如果他正在编写的函数没有返回副本(因此在这两种情况下都要复制

副本),那么你的建议会更有意义。

如果没有明显的差异,这就是为什么

Victor正在寻找更多的洞察力(比他已经拥有的!)。


-Howard



I think you missed what I pointed out above, that both versions are making
copies. If he were writing functions that didn''t return a copy (thus making
the copy neccessariy in both cases), then your advice would make more sense.
In this case, it''s not apparent that there is a difference, which is why
Victor is looking for more insight (than he already has!) into the problem.

-Howard





Victor Bazarov写道:


Victor Bazarov wrote:

现在,我的问题是,为什么我更喜欢/保留使用传递给函数的值的一种方式,还是应该保留两者?您是否看到地表下的任何珊瑚礁,一种方法的任何潜在问题或同一项目中的两种方法?例如,优化器会绊倒其中一种方法而不是其他方法吗?

我会很感激任何见解。谢谢!

Now, my question is, why would I want to prefer/preserve one way of using
values passed to the function or should I keep both? Do you see any reefs
under the surface, any potential issues with one method or with having
both methods in the same project? Would the optimizer stumble on one of
the methods and not the other, for example?

I''d appreciate any insight. Thanks!



我们( http://tinyurl.com/ 7y38k )更喜欢表格


void func(const myClass& input,myClass& output);


我们不鼓励复制构造默认情况下,我们的

make-a-new类脚本使这两种方法都是私有的。为什么?好吧

我们的课程可以包含100Mb +的数据,我们不希望那些被复制或分配不必要的那些,我们将不遗余力地

避免有人无意中写作:


myClass func(myClass输入);


We (http://tinyurl.com/7y38k) prefer the form

void func(const myClass& input, myClass& output);

we discourage copy construction and assignment by default, our
make-a-new class script makes both of those methods private. Why? Well
our classes can contain 100Mb+ of data and we don''t want those being
copied about or assigned unnecessarily, this we''ll go to any lengths to
avoid someone inadvertantly writing:

myClass func(myClass input);


这篇关于通过const&amp;并返回临时值与传递值和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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