Boost logo

Boost :

Subject: Re: [boost] Looking for thoughts on a new smart pointer: shared_ptr_nonnull
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2013-10-09 04:42:39


On 08-10-2013 20:38, Daniel James wrote:
> On 8 October 2013 02:09, Rob Stewart <robertstewart_at_[hidden]> wrote:

>> There's no need to check for null, so there's no need to risk pipeline stalling on a failed branch prediction or cache invalidation due to the exception construction and throwing code you'd add to the ctor otherwise.
>
> Well, there's premature optimization for you.
>
> This is a class that's concerned with safety, not efficiency. If
> there's a safe option and an unsafe option, then the safe option
> should be the default, and the unsafe option should be the verbose
> one.

This is an important point. There is virtually no overhead of validating
the invariant. Why? Because the heap-allocation is going to
dwarf the validation. The cost of the validation will be regained
quickly because we do not need to check for null every time we access
the pointer.

OTOH, if std::vector<T>::front() validated it's precondition, it would
have a noticable runtime overhead.

Different situations. If it's cheap or free to avoid UB, why not do it?

-Thorsten


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