通过价值和临时性的论证 [英] argument passing by value and temporaries

查看:77
本文介绍了通过价值和临时性的论证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习C ++语言..

我读过很多甚至试图理解标准说的方式但仍然是

不能掌握这个概念。


当参数传递/返回时,临时值是

创建了吗?(我还没有触及那些情况标准允许从实现方面优化
以避免复制)

如果是这样,请引用标准部分说明。

假设确实如此,我可以想象两种情况,例如:


struct X {

};


无效foo(X b);


....

int main(int argc,char * argv []){

X a;

foo(a); //第一个案例

foo(X()); //第二个

}


在第一种情况下创建的临时变量是* copy *

用''构造一个''对象和一个函数调用''b''对象本身就是用这个临时变量构造的
副本..我做得对吗

假设iff(当参数是通过/返回值

临时创建)?

所以复制.ctor在这里使用了两次?


在第二种情况X()表达式是类型X的右值..我不是

确定在这里使用的术语... X()表达式不是

临时对象对吗?它只是一个右值(即值初始化)

和类型X的临时对象是* copy *使用该rvalue构造

然后''b''对象是还复制了那个临时的......


同样适用于返回的对象吗?


现在,如果一切都成立,执行者可以避免两个阶段

复制,就像在第一个例子中一样,用

'''''对象复制构造临时,然后用那个临时复制构造''b''对象,

''b''对象是用''''对象直接复制的?


在第二种情况下X()没有'''创建临时但直接复制

构造''b''?


TIA。


-

大卫

解决方案


da ***** @ mail.ru 写道:


我刚开始学习C ++语言..

我读过很多甚至试图理解标准说的方式但仍然无法获得b $ b这个概念。


当参数通过值临时值传递/返回时,是否创建了?b $ b?(我还没有触及标准允许的情况

从实现方面进行优化以避免复制)

如果是这样,请引用标准部分说明。

假设它是真的,我可以想象两种情况,例如:


struct X {

};


void foo(X b);


...

int main(int argc,char * argv []){

X a;

foo(a); //第一个案例

foo(X()); //第二个

}


在第一种情况下创建的临时变量是* copy *

用''构造一个''对象和一个函数调用''b''对象本身就是用这个临时变量构造的
副本..我做得对吗

假设iff(当参数是通过/返回值

临时创建)?



我不相信C ++标准需要创建一个临时变量

然后使用它来初始化函数参数和副本

构造函数。它应该只需要初始化函数

参数,复制构造函数被传递给函数参数。

你在哪里知道临时创建?你可以引用

a标准吗?


至于实际实施,与标准相反,当你运行时

所述代码只有一个默认构造函数用于初始声明

''a'',并且当你传递参数时,一个复制构造函数调用是实际用gcc制作的
。 br />

-

伊万
http://www.0x4849.net




Ivan Novick写道:

da*****@mail.ru 写道:


我刚刚开始学习C ++语言..

我读过很多甚至试图理解标准的说法,但仍然是

无法掌握这个概念。


当参数传递/返回时,临时值是

创建了吗?(我是没有头脑ching但标准允许从实现方面优化的情况以避免复制的情况下

如果是这样,请引用标准的部分说明。

假设它是真的,我可以想象两种情况,例如:


struct X {

};


void foo(X b);


...

int main(int argc,char * argv []) {

X a;

foo(a); //第一个案例

foo(X()); //第二个

}


在第一种情况下创建的临时变量是* copy *

用''构造一个''对象和一个函数调用''b''对象本身就是用这个临时变量构造的
副本..我做得对吗

假设iff(当参数是通过/返回值

临时创建)?



我不相信C ++标准需要创建一个临时变量

然后使用它来初始化函数参数和副本

构造函数。它应该只需要初始化函数

参数,复制构造函数被传递给函数参数。

你在哪里知道临时创建?你可以引用

a标准吗?


至于实际实施,与标准相反,当你运行时

所述代码只有一个默认构造函数用于初始声明

''a'',并且当你传递参数时,一个复制构造函数调用是实际用gcc制作的
。 br />

-

伊万
http://www.0x4849.net



当我在NG搜索这类问题时,恕我直言已经

被问到很多次,我在很少的帖子中看到,当按值传递/返回参数时,临时数据是创建的。我现在找不到这些

的帖子,但无论如何都重要吗?我没有说我读过它

来自标准,我只是问它是否真的在标准中定义了

方式...很抱歉我的第一个不清楚发帖如果被理解

就是这样..


你能不能提供一个不需要的标准的诗歌

?或者任何其他声明暗示临时值不会在参数传递/按价值计算时产生
。我真的很困惑

这件事我需要清楚解释标准也许

的例子......


提前谢谢。


-

David




" Ivan Novick" < iv ** @ 0x4849.net写信息

news:11 ********************** @ t46g2000cwa.googlegr oups。 com ...


>
da **** *@mail.ru 写道:


>我刚开始学习C ++语言..
我读了很多甚至试图理解标准所说的方式,但仍然无法掌握这个概念。

当参数传递/返回时,临时值被创建了吗?(我还没有触及标准允许从实现方面进行优化以避免复制的情况。
如果是这样,请引用标准的部分内容。
假设它是是的,我可以想象两个案例如:

struct X {
};

void foo(X b);

...

X a;
foo(a); //第一种情况
foo(X()); //第二个


在第一种情况下,临时变量被创建为* copy *
由''''对象和函数调用''b'构成'对象本身是用这个临时变量构造的副本..我做得对吗
假设iff(当参数通过值传递/返回时创建临时变量)?



我不相信C ++标准需要创建一个临时变量

然后使用它来初始化函数参数和副本

构造函数。它应该只需要初始化函数

参数,复制构造函数被传递给函数参数。

你在哪里知道临时创建?你可以在标准中引用

a经文吗?



好​​吧,它确实会创建一个临时对象,但该对象是参数

本身。它没有创建一个临时的,然后从

创建参数。


至于实际实现,而不是标准,当你运行

表示代码只有一个默认构造函数用于初始声明

''a'',并且当你传递参数时一个复制构造函数调用是

实际用gcc制作。



是的。


-Howrad


Hi, I have just started learning C++ language..
I''ve read much even tried to understand the way standard says but still
can''t get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I''m not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

....

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}

In the first case temporary variable is created that is *copy*
constructed with ''a'' object and upon function call ''b'' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
So copy .ctor is used two times here ?

In the second case X() expression that is rvalue of type X.. I''m not
sure as well about the terminology to use here... X() expression is not
temporary object right? it is just a rvalue(that is value initialized)
and temporary object of type X is *copy* constructed with that rvalue
and then ''b'' object is also copy constructed from that temporary...

Same holds true for returning objects ?

Now, if everything holds true, implementators can avoid two phase
copying, like in first example instead to copy construct temporary with
''a'' object and then to copy construct ''b'' object with that temporary,
''b'' object is directly copy constructed with the ''a'' object?

And in second case X() doesn''t create temporary but directly copy
constructs ''b'' ?

TIA.

--
David

解决方案


da*****@mail.ru wrote:

Hi, I have just started learning C++ language..
I''ve read much even tried to understand the way standard says but still
can''t get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I''m not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

...

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}

In the first case temporary variable is created that is *copy*
constructed with ''a'' object and upon function call ''b'' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?

I don''t believe the C++ standard requires creating a temporary variable
and then using it to initialize the function parameter with the copy
constructor. It should require only initializing the function
parameter with the copy constructor being passed the function argument.
Where did you get the idea that a temporary is created? Can you quote
a verse in the standard?

As for actual implementation, as opposed to the standard, when you run
said code only one default constructor for your initial declarion of
''a'', and one copy constructor call when you pass the argument is
actually made in gcc.

--
Ivan
http://www.0x4849.net



Ivan Novick wrote:

da*****@mail.ru wrote:

Hi, I have just started learning C++ language..
I''ve read much even tried to understand the way standard says but still
can''t get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I''m not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

...

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}

In the first case temporary variable is created that is *copy*
constructed with ''a'' object and upon function call ''b'' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?

I don''t believe the C++ standard requires creating a temporary variable
and then using it to initialize the function parameter with the copy
constructor. It should require only initializing the function
parameter with the copy constructor being passed the function argument.
Where did you get the idea that a temporary is created? Can you quote
a verse in the standard?

As for actual implementation, as opposed to the standard, when you run
said code only one default constructor for your initial declarion of
''a'', and one copy constructor call when you pass the argument is
actually made in gcc.

--
Ivan
http://www.0x4849.net

When I was searching NG for this type of question that IMHO has been
asked many times, I saw in few of the posts that said temporaries are
created when passing/returning arguments by value.. I can''t find these
posts right now, but does it matter anyways? I didn''t say I read it
from standard, I just asked if it was really defined in standard that
way... Sorry for being unclear in my first post if it was understood
like that..

And can you provide a verse in the standard where it doesn''t require so
? Or any other statement that implies that temporaries are not made
during argument passing/returing by value. I''m really confused about
that matter and I''m in need of clear explaination of standard maybe
with examples too...

Thank you in advance.

--
David



"Ivan Novick" <iv**@0x4849.netwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com...

>
da*****@mail.ru wrote:

>Hi, I have just started learning C++ language..
I''ve read much even tried to understand the way standard says but still
can''t get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I''m not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

...

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}

In the first case temporary variable is created that is *copy*
constructed with ''a'' object and upon function call ''b'' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?

I don''t believe the C++ standard requires creating a temporary variable
and then using it to initialize the function parameter with the copy
constructor. It should require only initializing the function
parameter with the copy constructor being passed the function argument.
Where did you get the idea that a temporary is created? Can you quote
a verse in the standard?

Well, it does create a temporary object, but that object is the parameter
itself. It doesn''t create a temporary and then create the parameter from
that.

As for actual implementation, as opposed to the standard, when you run
said code only one default constructor for your initial declarion of
''a'', and one copy constructor call when you pass the argument is
actually made in gcc.

yep.

-Howrad


这篇关于通过价值和临时性的论证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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