Boost logo

Boost :

Subject: Re: [boost] std::map::find() wrapper
From: Jeremy Maitin-Shepard (jeremy_at_[hidden])
Date: 2011-05-05 18:23:26


On 05/05/2011 03:06 PM, John Bytheway wrote:
[snip]
> Because static_cast<C*>(b) is valid when b == 0, and must return 0, but
> any non-zero value must be changed (by subtracting 4) because of the
> multiple inheritance. This is a pretty obscure corner case, but it does
> demonstrate that the non-NULL guarantee for references has at least some
> performance implications.

I think it is fairly obvious that knowing a pointer to be non-null can
be helpful to the optimizer. However, this example does not seem
relevant to the discussion of the find function, since the whole point
is that the pointer returned may indeed be null and therefore should be
checked. I imagine you'll find with most compilers that code like:

C *c = find(...);
if (c) {
   B *b = static_cast<B*>(c);
   // do something with b
}

will result in only a single null pointer check.


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