std::map <int, std::shared_ptr<A>> m; std::shared_ptr<A> *a = &m[2]; std::shared_ptr<A> *b = &m[2]; m.erase(2); if(a) std::cout << "exist a\n"; if(b) std::cout << "exist b\n"; 

Is it possible to somehow find out with minimal costs (there are many elements in the map) does m [2] exist? Is it possible to somehow know this through a and b? Is it possible to somehow reset / delete the memory to which they refer and thus to perform the verification when deleting m [2]?

  • 2
    Um ... Somehow I’m straining to use a pointer to shared_ptr ... Not in the sense of language capabilities, but in the sense of design, of course ... - Harry
  • @Harry And it doesn't bother me. And the problems of design will be solved by me separately and not here. If you think that this is all code, then you are deeply mistaken. If there is something to say / add on the question - you are welcome. Even you can and about the design, about how you personally see the solution. But do not write about your voltage - it is not interesting. And you can lose your stress ... minus the question. - Max ZS
  • 3
    You have an interesting reaction ... some kind of inadequate. About the design - you can. If you know the task. It’s just that shared_ptr itself was created to get rid of pointers. By the way, the proposed solution does not use them! Therefore, the pointer to shared_ptr ... well, since you are in a bottle and you didn’t like my polite comment, so be it, I will tell you straight - the decision is stupid. And yet - do not tell me what I can write and what not, and I will not tell you where to go. To everyone who reads: forgive me generously, if there was a person there, I would write there, and you have to read it ... :( - Harry
  • @Harry Don't you think that your reaction to my question was fundamentally wrong? If you do not know the task, how could you write about the design, and now justify ignorance of the problem? Do not you think that this resource has the goal of questions and answers, and not a description of its tension on the basis of design conjectures? And don't you think that in order not to regret the lack of a personal one, you simply do not have to write stupid comments initially, then you don’t have to ask for forgiveness from the rest of the readers? - Max ZS
  • @Harry There are quite clear rules of the resource: there is something to write about the issue - write so. You think the question is not correct - minus it and write in the comments why you think so. And to write your speculation is best in their blogs. You can also say that take the best example from the author of the answer - he answered the question and, indirectly, solved the question about pointers to shaed_ptr, thereby "suggesting" a more correct design. - Max ZS

1 answer 1

You can store std::shared_ptr in std::map , and for a and b use std::weak_ptr :

 std::map <int, std::shared_ptr<A>> m; std::weak_ptr<A> a = m[2]; std::weak_ptr<A> b = m[2]; m.erase(2); if (!a.expired()) std::cout << "exist a\n"; if (!b.expired()) std::cout << "exist b\n"; 
  • Thank. Indeed :) Otherwise, I somehow rested against unique_ptr and shared_ptr and did not think at all (forgot) about weak_ptr. - Max ZS