本文介绍了对临时和函数调用的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct foo {

int i;

};


int bar(foo& f){

返回f.i ++;

}


int main(){

bar(foo()) ;

}


以上代码无法编译,因为你不能绑定对

临时的引用,你可以通过使用''const foo&''参数

来解决这个问题,但是你有其他问题(比如试图改变const的

值)。我理解这一点,我不明白的是

为什么这被认为是试图绑定一个临时的引用,

并非所有参数都应该是在功能执行之前评估了

?这些评估应该在与调用函数的

相同的范围内进行?虽然这个函数,包括它的

参数,都是在自己的范围内执行的?


我看东西的方式是foo() - bar的一部分(foo) ());当

参数f进入范围*时,应该已经执行了

(因此在堆栈上创建了一个foo对象),因此,从

bar()是非临时的。


或换句话说,我不能完全看出
$ b之间的区别$ b以下两个:

bar(foo());



foo f;

bar( f);


有人可以解释一下吗?


*或者声明,定义或者叫什么。


-

Erik Wikstr?m

struct foo {
int i;
};

int bar(foo& f) {
return f.i++;
}

int main() {
bar(foo());
}

The above code does not compile since you can''t bind a reference to a
temporary, you could solve this by using a ''const foo&'' parameter
instead but then you have other problems (like trying to change the
value of a const). This much I understand, what I don''t understand is
how come this is considered trying to bind a reference to a temporary,
are not all parameters supposed to be evaluated before the function is
executed? And these evaluations should take place in the same scope as
in which the function is called? While the function, including its
parameters, are executed in its own scope?

The way I see things the foo()-part of bar(foo()); should already have
executed (and thus have created a foo-object on the stack) when the
parameter f comes into scope* and thus, from the point of view of
bar() be non-temporary.

Or put another way, I can''t quite see the difference between the
following two:
bar(foo());
and
foo f;
bar(f);

Can someone please explain?

* Or is declared, or defined or whatever it is called.

--
Erik Wikstr?m

推荐答案



我不确定还有什么*可以理解。

I''m not sure there is anything else *to* understand.



因为临时对象的定义包括foo对象

创建


bar(foo());

Because the definition of "temporary object" includes the foo object
created in

bar(foo());



对所有三个都是肯定的,这些都不会影响foo对象

由语句栏创建的事实(foo());是一个临时对象。

Yes to all three, none of which affect the fact that the foo object
created by the statement bar(foo()); is a temporary object.



是的。一个未命名的临时foo对象现在存在于main的范围内。

Yes. An unnamed temporary foo object now exists in the scope of main.



酒吧的范围并不是你应该想的。假设你

重写吧取一个const foo&所以你的代码编译,没有

方式(据我所知)你可以从内部告诉* * f引用的

对象是否是临时或不在

调用函数范围内。但是从酒吧内部来看并不重要。什么'

重要的是来自调用bar的函数内部(主要在这个

的情况下)。


,有一个临时的foo对象被创建,你不能
绑定对临时对象的非const引用。如果我在你的主

函数中添加一行,那么你就有...


int main(){

bar( foo());

foo& a_reference = foo();

}


....两个语句都有同样的问题。他们都尝试将

临时绑定到非const引用。事实上,在第一种情况下

非const参考恰好是一个函数参数并不是很重要。在这两种情况下,*引用的对象*都是临时的,所以

引用必须是const。

The scope of bar isn''t where you should be thinking. Assuming you
rewrite bar to take a const foo& so your code compiles, there is no
way (as far as I know) that you can tell *from inside bar* whether the
object referred to by f is a temporary or not in the scope of the
calling function. But from inside bar isn''t what''s important. What''s
important is from inside the function that calls bar (main in this
case).

Inside main, there is a temporary foo object created and you can''t
bind non-const references to temporaries. If I add a line to your main
function so you have...

int main() {
bar(foo());
foo& a_reference = foo();
}

....both statements have the same problem. They both try and bind a
temporary to an non-const reference. The fact that in the first case
the non-const reference happens to be a function parameter doesn''t
matter. In both cases the object *referred to* is a temporary so the
reference has to be const.



也许你现在可以吗?


HTH

Gavin Deane

Maybe you can now?

HTH
Gavin Deane




我不确定还有什么*可以理解。


I''m not sure there is anything else *to* understand.



因为临时对象的定义包括foo对象

创建


bar(foo());


Because the definition of "temporary object" includes the foo object
created in

bar(foo());



对所有三个都是肯定的,这些都不会影响foo对象

由语句栏创建的事实(foo());是一个临时对象。


Yes to all three, none of which affect the fact that the foo object
created by the statement bar(foo()); is a temporary object.



是的。一个未命名的临时foo对象现在存在于main的范围内。


Yes. An unnamed temporary foo object now exists in the scope of main.



酒吧的范围不是你应该考虑的地方。假设你

重写吧取一个const foo&所以你的代码编译,没有

方式(据我所知)你可以从内部告诉* * f引用的

对象是否是临时或不在

调用函数范围内。但是从酒吧内部来看并不重要。什么'

重要的是来自调用bar的函数内部(主要在这个

的情况下)。


,有一个临时的foo对象被创建,你不能
绑定对临时对象的非const引用。如果我在你的主

函数中添加一行,那么你就有...


int main(){

bar( foo());

foo& a_reference = foo();


}


...这两个语句都有同样的问题。他们都尝试将

临时绑定到非const引用。事实上,在第一种情况下

非const参考恰好是一个函数参数并不是很重要。在这两种情况下,*引用的对象*都是临时的,所以

引用必须是const。


The scope of bar isn''t where you should be thinking. Assuming you
rewrite bar to take a const foo& so your code compiles, there is no
way (as far as I know) that you can tell *from inside bar* whether the
object referred to by f is a temporary or not in the scope of the
calling function. But from inside bar isn''t what''s important. What''s
important is from inside the function that calls bar (main in this
case).

Inside main, there is a temporary foo object created and you can''t
bind non-const references to temporaries. If I add a line to your main
function so you have...

int main() {
bar(foo());
foo& a_reference = foo();

}

...both statements have the same problem. They both try and bind a
temporary to an non-const reference. The fact that in the first case
the non-const reference happens to be a function parameter doesn''t
matter. In both cases the object *referred to* is a temporary so the
reference has to be const.



也许你现在可以吗?


Maybe you can now?



对不起但不是,在这两种情况下,foo-object都是临时的,但在一个

中有一个名字而在另一个没有。我想我有点想要找到这种行为的理由,而且我能想到的唯一一件事就是如果它被允许你会丢失一个

优化的机会,即能够在函数的堆栈框架中创建复制参数

,而这是不可能的

,如果使用了参考文件。


我看不出当前行为对我所描述的那个有什么好处,我也看不出理由为什么不应该实现

实现(尽管,不可否认我不是编译器开发人员)。在另一方面,我可以看到一些允许我描述的行为的用法,

等标准库中的许多算法

可能会变得更有用。


-

Erik Wikstr?m

Sorry but no, in both cases the foo-object is temporary, but in one
there''s a name and in the other there isn''t. I guess I''m kind of
looking for a rationale for this behaviour, and the only thing I can
think of is that if it were allowed you would lose an opportunity to
optimize, namely the ability to create the copied parameter in place
in the stackframe of the function, whereas that would not be possible
if a reference was used.

I just can''t see any advantage of the current behaviour over the one I
described nor can I see reason why it should not be possible to
implement (though, admittedly I''m no compiler developer). On the other
hand I can see some usages of allowing the behaviour I described,
among other things a number of algorithms in the standard library
could become more useful.

--
Erik Wikstr?m




另外请记住,对于这两个版本,如果引用是const,则foo的copy ctor必须是可访问的。事实上,这确实有效。


int main()

{

foo& a_reference(foo());

}


即使没有可访问的副本ctor。这些事情让我感到奇怪。在什么

的情况下需要一个副本来将临时绑定到(const)引用?

如果[foo& a = foo(); ]是不允许的,为什么[foo&一个(FOO()); ]
允许
?为什么后者_not_需要复印件?


- 西尔维斯特

Also keep in mind that for both versions the copy ctor of foo has to be
accessible if the reference were to be const. And this, in fact, does work

int main()
{
foo& a_reference(foo());
}

Even without accessible copy ctor. These things strike me as odd. In what
situations a copy is needed to bind the temporary to a (const) reference?
And if [ foo& a = foo(); ] is not allowed, why is [ foo& a(foo()); ]
allowed? And why does the latter _not_ require a copy ctor?

- Sylvester


这篇关于对临时和函数调用的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:22