Boost logo

Boost :

Subject: Re: [boost] [optional] Thoughts on disallowing assignment for wrapped references.
From: Nevin Liber (nevin_at_[hidden])
Date: 2011-09-06 14:28:54

On 6 September 2011 04:52, Mostafa <mostafa_working_away_at_[hidden]> wrote:
> 1) Raw Pointers
> Raw pointers don't convey the semantics of their life-time policy,

It is stronger than that. Raw pointers don't have a lifetime policy.

> 2) Smart Pointers
> Smart pointers are nice in that they convey the semantics of their
> life-time,

They don't just convey them; they enforce them.

> but their have been many discussions on this forum which have
> advised against passing or returning a dereferenced smart pointer by
> reference.

That sounds like bad advice (and I've said so on previous occasions),
at least as far as passing in parameters to a function.

My advice is simple: if the callee isn't mucking with
lifetime/ownership, do not pass in a smart pointer; rather, pass in a
raw C++ reference or pointer.

Returning something, by definition, mucks with lifetime (since you
want to extend it past the lifetime of the callee, and you have no
idea how long the caller wants to extend it past that), so you want to
return a smart pointer.

> If we were to follow the latter advice, and I am inclined to do
> so,

Um, okay I guess. But by following that advice, you end up making
these issues far more complicated than they need to be.

> then smart pointers become a noop for the aforementioned idiom.  And
> this creates a new problem, in that now smart pointers don't convey the
> semantics of their "optionalness".

Like I said, complicated...

Just for fun: in your world, what would you expect the semantics of
optional2< optional2<T&> > to be? :-)

 Nevin ":-)" Liber  <mailto:nevin_at_[hidden]>  (847) 691-1404

Boost list run by bdawes at, gregod at, cpdaniel at, john at