Suppose I have a bunch of A* allocated that I want to pass as an argument to boost::bind . boost::bind stored for later processing in some container of type STL boost::functions .
I want A* be destroyed when the STL container is destroyed.
To demonstrate:
A* pA = new A(); // some time later container.push_back(boost::bind(&SomeClass::HandleA, this, pA); // some time later container is destroyed => pA is destroyed too
How can I do that?
EDIT
Perhaps what I want is not so realistic.
I have a raw pointer and a function that receives a raw pointer. The call is delayed using boost :: bind . At this point, I want automatic memory management in case boost :: bind wants to execute. I am lazy, so I want to use a “turnkey” solution for smart pointers.
std :: auto_ptr looks like a good candidate, however ...
auto_ptr<A> pAutoA(pA); container.push_back(boost::bind(&SomeClass::HandleA, this, pAutoA);
not compiled (see here )
auto_ptr<A> pAutoA(pA); container.push_back(boost::bind(&SomeClass::HandleA, this, boost::ref(pAutoA));
pAutoA is destroyed by removing the underlying pA.
EDIT 02
In the specified container, I will need to store different "callbacks" with different arguments. Some of them are raw pointers to an object. Since the code is old, I cannot always change it.
Writing your own wrapper to store callbacks in the container is the last resort (although perhaps the only one), therefore, the reward.