lundi 18 décembre 2017

Iterating over dereferenced unique_ptr, that contains vector, in for range loop

Why doesn't this code work as I supposed?

for (auto it: *std::make_unique<std::vector<int>>(std::vector<int>({1, 2, 3, 4, 5})))
    std::cout << it << std::endl;

The vector object is destroyed before executing the first iteration of the loop

Aucun commentaire:

Enregistrer un commentaire