In my program I have c++ class objects that keep SmartPointers members (SmartPointer is my own custom class derived from boost::shared_ptr). By design, some of my class objects must keep SmartPtr that are unique i.e no shared ownership is allowed.
I want to implement check module for debug reasons that will test whether given c++ class object (the c++ object can contain nested c++ class object members on its own) keeps smart pointers with shared ownership. If yes, I want to throw exception. I was thinking to reuse the serialization lib for this purpose since I already have serialize functions for all my classes. All I have to do is add checking code in my SmartPointer::serialize function to test whether the pointer was already saved in the archive. So anyone is aware of such function in boost serialization that will tell me whether pointer object was already serialized? I think that the boost serialization library must have mechanism to check this, since in the archive output shared_ptrs with shared ownerships are written just once.
The code smell is using a shared_ptr where unique ownership must be guaranteed. What are you going to do when you find a violation? Assert?
std::terminate?You could make a serialization wrapper that adds the check on de-serialization.
You will depend on library implementation details, because - apparently - while loading there will be a temporary copy of the shared_ptr:
Live On Coliru
Prints
Summary
Don't do this, because you can't cleanly do it. The alternative is far worse (delving into the object tracking implementation details as some commenters suggested).
Simply