What is required is a container object which either is empty or holds a single reference to a specified type. The reference must be popped out when its lifetime ends (just a pop() like method is enough).
First thing that comes to mind is a vector (or a vecdeque or a stack), simply push the reference and before it goes out of scope, pop it.
Any better suggestions for this (more efficient methods probably)? Is it possible to free the variable inside a Box object then use the box later?