IE what happens if you have this following piece of code?
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?
Also, how does this work/interfere with RVO?
Thanks in advance.
EDITED: Added const, forgot to put that in.