0

Function GetVec return std::vector. Then I store the returned value into the const ref. Does the returned value stored in const ref get destroyed in next line. I was in the impression it wont.


#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);
}

Output :

1,2,3,

mato
  • 503
  • 8
  • 18

0 Answers0