title: Soft (not: weak) references in C++ - Is it possible? Is there an implementation?
body: <p>In C++ I'm using <code>boost::shared_ptr</code> and <code>boost::weak_ptr</code> to automatically delete objects that are no longer needed. I know these work with reference counting.</p>
  
  <p>In Java, memory is managed by a garbage collector, which consideres the built-in object references as <em>strong</em>, <code>WeakReference</code> as <em>weak</em> and <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ref/SoftReference.html" rel="noreferrer" title="Javadoc Page for java.lang.ref.SoftReference"><code>SoftReference</code></a> as something in between (may be collected by the GC, but may as well survive the GC), which is really handy for caching objects for some time, but throwing them away as soon as free memory is getting low.</p>
  
  <p>So now I'm back in C++ and I miss the comfort of having soft references. I wonder if soft referencing is practicable with reference counting at all. When the last <em>strong</em> reference to an object is cleared, and there remains a <em>soft</em> reference, when would it be deleted after all? I could think of some schemes, but none of them seem clever to me.</p>
  
  <p>Just in case there are proper semantics for soft references along with reference counting, I wonder if this has already been implemented, maybe in a way that's even compatible with <code>boost::shared_ptr</code> (or the C++ TR1 equivalent <code>std::shared_ptr</code> for that matter).</p>
  
  <p>If the answer to both questions is no, what are the alternatives in an object caching scenario?</p>
  
  <p><strong>EDIT:</strong>
  Of course I'm talking of a situation when caching is actually useful, because the objects are <em>costly</em> to construct (think of several access to a database and queries of a network), yet there are too many to keep them all forever.</p>