|
Boost : |
Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2012-02-14 14:53:34
Simonson, Lucanus J wrote:
> Hite, Christopher
>
> >Any C++ programmer reading your code should understand what
> > T* means. He may not be familiar with boost::optional<T&>.
>
> The real question is, is optional<T&> safer than T*? If
> someone accesses the object inside optional without checking
> if it is valid that is equivalent to dereferencing a pointer
> without checking if it is null. However, safety is about
> behavior, and people tend to dereference pointers without
> checking them very frequently because there are plenty of
> cases where it is safe to assume a pointer is not null.
I don't know to what "people" you're referring, but I check for null pointers before dereferencing them. If you want to pass the buck, always use smart pointers that validate dereferences. However, I prefer not to pay the overhead of checking for null on every dereference, so I convert to a reference once I've verified a non-null pointer, even with smart pointers, provided there's to be more than one dereference.
> In code review people ask me "why didn't you check if that
> pointer is null?" and my answer is usually, "because throwing
> an exception if it is null is the behavior I want". You
In that case, you're using a smart pointer.
> could say I should have used a reference instead, but it
> might not have been my pointer to start with.
What has that to do with it? Once I get a pointer into my code, I check for null, dereference it, and pass it by reference thereafter so no other code needs to test for null.
> Specifically, polymorphic data types are often passed around by
> pointer to base class instead of reference to base class by
> most C++ programmers.
Really? I'm certainly not "most C++ programmers" then. If something might not exist, I use optional or a pointer, depending. If it is required to exist, then I use a reference. I write functions taking references, not pointers, when I don't want to deal with the possibility of a null pointer. The caller must handle that for me.
_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP http://www.sig.com
________________________________
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk