[英]What happens in C++ if you pass an anonymous object into a function that takes a reference?
IE what happens if you have this following piece of code? IE如果您有以下代码,会发生什么?
int mean(const vector<int> & data) {
int res = 0;
for(size_t i = 0; i< data.size(); i++) {
res += data[i];
}
return res/data.size();
}
vector<int> makeRandomData() {
vector<int> stuff;
int numInts = rand()%100;
for(int i = 0; i< numInts; i++) {
stuff.push_back(rand()%100);
}
}
void someRandomFunction() {
int results = mean(makeRandomData());
}
Am I correct in thinking that C++ will just preserve the newly created object for the life of mean, and then destroy it afterwards since it goes out of scope? 我是否正确地认为C ++将保留新创建的对象为平均生命,然后在它超出范围之后将其销毁?
Also, how does this work/interfere with RVO? 此外,这如何工作/干扰RVO?
Thanks in advance. 提前致谢。
EDITED: Added const, forgot to put that in. 编辑:添加const,忘了把它放进去。
My psychic powers tell me that you're compiling this on Visual C++, which is why it even works. 我的通灵能力告诉我你在Visual C ++上编译它,这就是为什么它甚至可以工作。 In standard C++, you cannot pass an rvalue (which is what the return value of
makeRandomData
is) to a reference-to-non-const, so the question is moot. 在标准C ++中,您不能将rvalue(这是
makeRandomData
的返回值)传递给引用到非const,因此问题没有实际意义。
However, the question is still valid if you change the signature of mean
to take a const vector<int>&
. 但是,如果您更改
mean
的签名以获取const vector<int>&
,则该问题仍然有效。 In which case it all boils down to the lifetime of the temporary - which is defined to last until the end of the "full expression" in which it occurs. 在这种情况下,它都归结为临时的生命周期 - 它被定义为持续到它出现的“完整表达式”结束。 In your particular case, the full expression is the entire initializer of
results
. 在您的特定情况下,完整表达式是
results
的整个初始化程序。 In case of an expression statement, the full expression is that entire statement. 在表达式语句的情况下,完整表达式是整个语句。
The Standard does not specify any way in which function arguments can inhibit RVO, but, of course, RVO is a mandate to the compiler to do a particular optimization regardless of visible side effects, not a requirement to do it. 该标准不指定任何方式,其中函数的参数可以抑制视网膜静脉阻塞,但是,当然,RVO是其任务是编译器做了特殊的优化,无论可见副作用,不这样做的必要条件 。 When (and if) RVO happens is entirely up to the specific compiler you're using.
何时(以及如果)RVO发生完全取决于您正在使用的特定编译器。 That said, there does not seem to be any reason why it should be affected by this in any way.
也就是说,似乎没有任何理由可以以任何方式影响它。
In C++ you can't pass a temporary object to a function that takes a non-constant reference. 在C ++中,您无法将临时对象传递给采用非常量引用的函数。 The example you posted above will not compile.
您在上面发布的示例将无法编译。 If your compiler compiles it, it is a quirk (extension) of your compiler.
如果您的编译器编译它,它是编译器的一个怪癖(扩展)。 If your compiler compiles it without even issuing a diagnostic message, your compiler is broken.
如果编译器在没有发出诊断消息的情况下编译它,那么编译器就会崩溃。
A temporary object (result of your makeRandomData()
call), can only be passed to mean
through a const-qualified reference. 临时对象(您的结果
makeRandomData()
调用),只能传给mean
通过一个常量限定参考。 Ie your mean
has to be declared as 即你的
mean
必须被宣布为
int mean(const vector<int> & data)
in which case the reference will be bound either directly to the temporary object returned by makeRandomData()
, or to another copy of that temporary object the compiler might choose to create. 在这种情况下,引用将直接绑定到
makeRandomData()
返回的临时对象,或绑定到编译器可能选择创建的临时对象的另一个副本。 (The latter is unlikely in this case, but might happen in theory.) (在这种情况下,后者不太可能,但理论上可能会发生。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.