jeudi 20 septembre 2018

Return vector as auto from function

Is it possible to return std::vector as auto? For example:

auto retVec() {
  std::vector<int> vec_l;

  l.push_back(1);
  l.push_back(2);

  return vec_l;
}
...
auto ret_vec = retVec();
for (auto& it : ret_vec) {
}

when I write something like this I get an error:

  1. error: use of ‘auto retVec()’ before deduction of ‘auto’ ---> auto ret_vec = retVec();
  2. error: unable to deduce ‘auto&&’ from ‘ret_vec ---> for (auto it : ret_vec) {

I'm not very good at c ++ 11/14/17, and maybe this code is stupid. But if someone knows how it can be written - it would be great to see.

regards, max

Aucun commentaire:

Enregistrer un commentaire