返回值优化需要复制构造函数吗? [英] Is a copy constructor needed for the return value optimization?

查看:69
本文介绍了返回值优化需要复制构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面的代码演示了moo的复制构造函数不是在main的第一行调用的
。尽管如此,g ++(版本4.1.2)

如果我将复制构造函数设为私有,则拒绝编译它。但

数字火星编译器并没有抱怨。


哪个编译器是正确的? (如果gcc是对的,为什么这个限制

存在?)


Szabolcs


#include< ; iostream>


struct moo {

int data;


moo(int n):data(n ){

std :: cout<< constructor \ n;

}


//私人:

moo(const moo& m): data(m.data){

std :: cout<< copy constructor \ n;

}

};


moo fun(){

返回moo(3);

}


int main(){

moo m = fun();

返回0;

}


The code below demonstrates that the copy constructor of moo is not
called on the first line of main. In spite of this, g++ (version 4.1.2)
refuses to compile it if I make the copy constructor private. But the
Digital Mars compiler does not complain.

Which compiler is right? (And if gcc is right, why does this restriction
exist?)

Szabolcs

#include <iostream>

struct moo {
int data;

moo(int n) : data(n) {
std::cout << "constructor\n";
}

// private:
moo(const moo &m) : data(m.data) {
std::cout << "copy constructor\n";
}
};

moo fun() {
return moo(3);
}

int main() {
moo m = fun();
return 0;
}

推荐答案

SzH写道:
SzH wrote:

>

下面的代码演示了moo的复制构造函数在main的第一行上没有调用
。尽管如此,g ++(版本4.1.2)

如果我将复制构造函数设为私有,则拒绝编译它。但

数字火星编译器并没有抱怨。


哪个编译器是正确的? (如果gcc是正确的,为什么这个限制存在?b $ b?)
>
The code below demonstrates that the copy constructor of moo is not
called on the first line of main. In spite of this, g++ (version 4.1.2)
refuses to compile it if I make the copy constructor private. But the
Digital Mars compiler does not complain.

Which compiler is right? (And if gcc is right, why does this restriction
exist?)



正如名称所说,返回值优化是一种优化。

优化不应该使代码编译错误,也不能使有效的代码无法编译.b $ b代码编译失败。按值返回结果,因为你的函数

确实需要复制值,这又需要一个可访问的

复制构造函数。当您使复制构造函数无法访问时,

无法按值返回该类型的对象。


-


- Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展:教程和
参考的作者。 ( www.petebecker.com/tr1book


Pete Becker写道:
Pete Becker wrote:

SzH写道:
SzH wrote:

>> <下面的代码演示了在main的第一行没有调用moo的拷贝构造函数。尽管如此,如果我将复制构造函数设为私有,g ++(版本
4.1.2)拒绝编译它。
但数字火星编译器不会抱怨。

其中编译是对的吗? (如果gcc是正确的,为什么存在这种限制?)
>>
The code below demonstrates that the copy constructor of moo is not
called on the first line of main. In spite of this, g++ (version
4.1.2) refuses to compile it if I make the copy constructor private.
But the Digital Mars compiler does not complain.

Which compiler is right? (And if gcc is right, why does this
restriction exist?)



正如名称所示,返回值优化是一种优化。

优化不应该使代码编译错误,也不能使有效的代码无法编译.b $ b代码编译失败。按值返回结果,因为你的函数

确实需要复制值,这又需要一个可访问的

复制构造函数。当您使复制构造函数无法访问时,

无法按值返回该类型的对象。


Return value optimization, as the name says, is an optimization.
Optimizations should never make incorrect code compile, nor make valid
code fail to compile. Returning a result by value, as your function
does, requires copying the value, which in turn requires an accessible
copy constructor. When you make the copy constructor inaccessible you
cannot return an object of that type by value.



我完全不同意这一点。如果它只是一个正常

优化,那么程序的结果不应该取决于是否执行优化

。当我运行该程序时,该行

" copy constructor"没有打印,所以我可以说优化

已经执行(即没有复制该值)。

I can not agree with this completely. If it was just a "normal"
optimisation then the result of the program should not depend on whether
the optimisation is performed or not. When I run the program, the line
"copy constructor" is not printed, so I can tell that the optimisation
was performed (i.e. the value was not copied).


SzH写道:
SzH wrote:

Pete Becker写道:
Pete Becker wrote:

> SzH写道:
>SzH wrote:

>>>
下面的代码演示了在main的第一行没有调用moo的复制构造函数。尽管如此,如果我将复制构造函数设为私有,g ++(版本
4.1.2)拒绝编译它。
但数字火星编译器不会抱怨。

其中编译是对的吗? (如果gcc是正确的,为什么存在这种限制?)
>>>
The code below demonstrates that the copy constructor of moo is not
called on the first line of main. In spite of this, g++ (version
4.1.2) refuses to compile it if I make the copy constructor private.
But the Digital Mars compiler does not complain.

Which compiler is right? (And if gcc is right, why does this
restriction exist?)


正如名称所说,返回值优化是一种优化。
优化永远不应该编写错误的代码,也无法使有效代码编译失败。正如您的
函数那样,按值返回结果需要复制值,而这又需要一个可访问的复制构造函数。当您使复制构造函数无法访问时,您无法按值返回该类型的对象。


Return value optimization, as the name says, is an optimization.
Optimizations should never make incorrect code compile, nor make
valid code fail to compile. Returning a result by value, as your
function does, requires copying the value, which in turn requires an
accessible copy constructor. When you make the copy constructor
inaccessible you cannot return an object of that type by value.



我完全不同意这一点。如果它只是一个正常

优化,那么程序的结果不应该依赖于是否执行优化。


I can not agree with this completely. If it was just a "normal"
optimisation then the result of the program should not depend on
whether the optimisation is performed or not.



标准明确允许这种优化,无论

是否存在构造函数中的副作用。顺便说一句,RVO只是

关于标准实际定义的唯一优化。并且它必须明确地说明无论副作用和b $ b部分。

The Standard explicitly allows this kind of optimization regardless
of the presence of side effects in the constructor. BTW, RVO is just
about the only optimization the Standard actually defines. And it
has to do that explicitly to state the "regardless of side effects"
portion.


当我运行程序时,

行复制构造函数没有打印,所以我可以说已经执行了

优化(即没有复制该值)。
When I run the program,
the line "copy constructor" is not printed, so I can tell that the
optimisation was performed (i.e. the value was not copied).



那么?它没有删除标准规定的要求。

代码格式错误_如果需要的话,副本*不能生成

它。


V

-

请在通过电子邮件回复时删除资金''A' />
我没有回复最热门的回复,请不要问

So? It doesn''t remove the requirement that the Standard sets forth.
The code is ill-formed _if_ the copy *cannot* be made should it be
necessary to make it.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于返回值优化需要复制构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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