mercredi 24 juin 2020

Insertion to std::list

I have a C++ snippet that looks like this:

  std::list<std::vector<int>> lv;
  lv.push_back({});
  std::cout << lv.size() << std::endl; // "1"

  lv.insert(lv.end(), {});  // Oops!
  // lv.insert(lv.end(), std::vector<int>());  // OK
  std::cout << lv.size() << std::endl; // Still got "1", but why?

As you can see, when using {} to create a default std::vector<int>, the insertion didn't happen at all. While I know that there are lots of subtleties when using C++'s initializer list, I wonder what is the problem in this example?

Thanks!

Aucun commentaire:

Enregistrer un commentaire