samedi 6 juin 2020

cannot reset std::shared_ptr object inside std::set<>... why?

In the below c++ calling reset() inside list, vector, map there is neither error nor warning.

however when I try to do it in set I get error.

error message is [ No matching member function for call to 'reset' ]

why this happened??? Can someone share your knowledge with the community??

std::shared_ptr<int> sp;
sp.reset(new int(11));
sp.reset();

std::map<int, std::shared_ptr<int>> my_map;
for (auto it = my_map.begin(); it != my_map.end(); ++it) {
  it->second.reset();
  (*it).second.reset();
}

std::list<std::shared_ptr<int>> my_list;
for (auto& x : my_list) {
  x.reset();
}

std::vector<std::shared_ptr<int>> my_vec;
for (auto it = my_vec.begin(); it != my_vec.end(); ++it) {
it->reset();
  (*it).reset();
}

std::set<std::shared_ptr<int>> my_set;
for (auto& x : my_set) {
  x.reset();      // ERROR!!!
}

for (auto it = my_set.begin(); it != my_set.end(); ++it) {
  it->reset();    // ERROR!!!
  (*it).reset();  // ERROR!!!
}
  • OS : Ubuntu18.04
  • Compiler: g++ 7.5.0-3ubuntu1~18.04

Aucun commentaire:

Enregistrer un commentaire