函数GetVec返回std :: vector。然后,我将返回的值存储到const ref中。存储在const ref中的返回值是否在下一行中被销毁。我印象中不会。
#include <vector>
#include <iostream>
std::vector<int> GetVec(){
std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
return vec;
}
void print(const std::vector<int>& arg){
for(const auto& id : arg){
std::cout <<id << ",";
}
std::cout << std::endl;
}
int main(){
const std::vector<int>& ret_val = GetVec();
// is content of ret_val undefined here?
print(ret_val);
}
输出:
1,2,3,