简体   繁体   English

如果将匿名对象传递给需要引用的函数,C ++中会发生什么?

[英]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.

相关问题 当您从 c++ function 返回引用时会发生什么? - What happens when you return a reference from a c++ function? 强制转换const以将其传递给具有引用功能的函数,会发生什么? - casting const to pass it to function that takes reference, what happens? C ++,为什么可以将右值传递给以左值引用为参数的函数 - C++, why can you pass rvalue to a function which takes lvalue reference as argument 当我通过引用传递一个对象并且它超出范围时,C ++会发生什么? - What happens in C++ when I pass an object by reference and it goes out of scope? 您可以将临时对象传递给通过引用获取变量的函数吗? - Can you pass a temporary object to a function that takes a variable by reference? 在C ++中为对象分配引用的值时会发生什么? - What happens when I assign an object the value of a reference in C++? 通过赋值初始化C ++对象时会发生什么? - What happens when you initialize a C++ object by assignment? 当C ++匿名对象替换先前分配的对象时会发生什么? - What happens when a C++ anonymous object replaces previously assigned object? 为什么或在什么情况下,您会将参数传递给函数作为C ++中的引用(或指针)? - Why, or in what situations, would you pass an argument to a function as a reference (or pointer) in C++? c ++中的对象调用不带参数的成员函数时会发生什么 - What happens when a member function with no arguments is called by an object in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM