Boost logo

Boost Users :

Subject: Re: [Boost-users] Usage of Boost.Optional
From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2010-11-27 19:57:49


On Sun, Nov 28, 2010 at 8:47 AM, Marsh Ray <marsh_at_[hidden]> wrote:
> On 10/28/2010 11:27 AM, John Dlugosz wrote:
>>
>> I think d.reset(in_place(args)) would do the trick, so I propose that
>> as a logical missing consequence of existing features.
>
> I was (independently) looking for this today and came across this recent
> thread. So I agree with you that it seems like natural functionality that is
> conspicuously missing.
>

I'm not sure you understood the resolution of the discussion -- you
will need to use the Boost.In_place_factory utility if you want to
construct the resource in-place.

> Optional even has a construct(in_place) method, but it's protected.
>

Just assign an in-place factory. Look at
http://www.boost.org/doc/libs/1_45_0/libs/utility/in_place_factories.html
for more details.

> I need to declare a variable but I'm not ready to construct it yet. The type
> represents an operating system resource so it doesn't have meaningful copy
> semantics and designing it differently is not possible.
>
> Std::auto_ptr would work except there's no good reason for this to require a
> heap allocation.
>
> Boost::optional is exactly what I want, it's just that it unnecessarily
> requires a copy for deferred initialization.
>

It's not required. You can use the in place factory for that.

optional<foo> o;
o = in_place<foo>(parameters, to, foo);

HTH

-- 
Dean Michael Berris
deanberris.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net