Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2003-09-05 14:06:02


On Friday 05 September 2003 02:43 pm, Ed Brey wrote:
> struct inert_bool_structure {};
> typedef inert_bool_structure const* inert_bool;

This allows several things we'd rather not allow:
  shared_ptr<T> p;
  p + 2; // not quite what we expected
  delete p; // dangerous
 
> 3. It avoids inefficiencies that other approaches impose. For instance,
> when I examine the optimized code generated by VC 7.1 from the technique
> used by shared_ptr et al, I find code that does a run-time fetch of the
> "unspecified" member address from a non-immediate variable. Another way to
> put it is that for shared_ptr, these two fragments generate different code:
>
> if (ptr.get()) ...
> if (ptr) ... // Less efficient.

IMHO, this is something we should document, not code around. If we can't get
the optimization without losing something (and we did lose something; see
above), we probably shouldn't do it because there is an easy workaround.

> What are people's thoughts of adding this to the utility library?

This is a tough thing to write a generic utility for. For instance, say both
the function and smart_ptr used this library: now one can compare
boost::function objects to boost::shared_ptr through the implicit conversion!
I'm sure I don't want to add this version to the library; when I'm not sure
about is if there is any version of this idiom that can be put into a utility
library.

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk