
Matt.Sullivan@leica-geosystems.com wrote:
Hi,
Probably a newbie question, but what's the best way to test if a shared_ptr and weak_ptr point to the same thing?
My current code uses: if (shared_ptr != weak_ptr.lock()) but it feels wrong to create a temporary with some small locking overhead - even though I realise it will make absolutely no difference to performance.
So, my question is really why is there no operator==(shared_ptr, weak_ptr)?
I can think of three reasons, none of them authoritative: 1. If this operator== ends up doing shared_ptr != weak_ptr.lock() under the covers, an argument could be made that the explicit version is better since it doesn't hide the overhead from the reader; 2. There are two sensible meanings for such an operator (as explained in N1590), "points to the same address" and "shares ownership with"; it might be better to leave the choice to the user and, again, to keep the choice explicit to benefit the reader; and last but not least 3. Nobody suggested the need for such an operator, so it wasn't included.
Google provided little explanation other than this: http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2004/n1590.html which approaches the subject but isn't exactly authoritative.
;-)