Boost logo

Boost :

Subject: Re: [boost] [optional] std::tr2::optional
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2011-11-20 03:15:30


On Saturday, November 19, 2011 00:16:43 Andrzej Krzemienski wrote:
> 3. T is neither copyable nor moveable -- it is like a scope guard, or
> boost::scoped_ptr. In this case optional<T> still provides assignment by
> making a "pseudo destructor call" followed by a call to placement new. --
> but this gives only basic guarantee.
> The question is if for this third variant we should be happy with only
> basic guarantee or to use a trick boost::variant is using with auxiliary
> heap-allocated object? (I do not even know if this is applicable though).

I think, heap-allocated storage is not acceptable since it defeats the whole
purpose of optional in this case. The user could have used unique_ptr (or
whatever other pointer) from the beginning to handle this case. What optional
provides is the effective in-place storage.

Basic guarantee is the best we can provide in this case and this is great.
Because normally there would be no assignment at all.

> > > The constructor will clash with another optional constructor:
> > > optional<T>::optional( bool cond, const T& val );
> >
> > I don't understand why.
>
> Currently boost::optional provides this constructor:
> optional<T>::optional( bool cond, const T& val );
> If cond is true the optional is initialized with object val, otherwise it
> is uninitialized.
>
> Now suppose I have a following type:
>
> struct MyType {
> bool on;
> int m1;
> int m2;
> MyType( bool on, MyType const& rhs )
>
> :on{on}, m1{rhs.m1}, m2{rhs.m2} {}
>
> };
>
> MyType mt { true, 1, 2 };
> boost::optional<MyType> o1{ true, mt }; // optional initialized with mt
> (copy ctor used)
> boost::optional<MyType> o2{ boost::in_place(true, mt) }; // uses 2-arg ctor
>
> But if std::tr2::optional has "variadic" constructor:
> std::tr2::optional<MyType> mt{ true, mt }; // uses 2-arg ctor

I think, this constructor with bool is excessive because there is no problem
in checking for the flag in the user's code before constructing the value.
This would even save the wasted construction of the value if the flag is
false.

What's really missing is the variadic assign method that would allow in-place
construction of values in an existing optional. So the example you described
would look like this:

  optional< MyType > opt;
  if (should_exist)
    opt.assign(true, 1, 2); // these arguments are passed to MyType ctor


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