Boost logo

Boost :

Subject: Re: [boost] [optional] generates unnessesary code for trivial types
From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2012-02-14 14:19:56


Hite, Christopher

>Any C++ programmer reading your code should understand what T* means. He may not be familiar with boost::optional<T&>.

Assuming a future std::optional, any future C++ programmer perhaps should know.

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. 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 could say I should have used a reference instead, but it might not have been my pointer to start with. Specifically, polymorphic data types are often passed around by pointer to base class instead of reference to base class by most C++ programmers. Optional, however, is only used when the value could be invalid under normal circumstances and would normally always be checked.

Regards,
Luke


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