Kiseong Yoo
Kiseong Yoo

Reputation: 97

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!!!
}

Upvotes: 0

Views: 302

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

The main problem is that std::set is an ordered container, and that ordering is done on insertion only.

To keep the order all keys in the set are constant and can't be modified.

You will have the same problem trying to modify the key of your std::map.

Upvotes: 5

Related Questions