Opened 15 years ago

Closed 8 years ago

#1841 closed Patches (fixed)

Optional: C++0x features

Reported by: jgottman@… Owned by: akrzemi1
Milestone: Boost 1.56.0 Component: optional
Version: Boost 1.36.0 Severity: Problem
Keywords: Cc:

Description

There are a couple of features in C++0x that would be very useful in the optional library. First is the emplace function (see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2345.pdf). This is how the C++0x containers will do in-place construction; optional should allow it as well so the same operation can be done in the same way. One issue is what to do if we call emplace on an optional that is not empty. We could assert, throw an exception, reset and then construct in-place, or construct out-of-place and then swap. Personally I prefer the third option.

Second, optional should make use of rvalue-references where available. Note that if swap() were implemented using the move constructor and move assignment operator, then it would just do the right thing with respect to iterators to the standard containers.

template<class T>
inline
void optional_swap ( optional<T>& x, optional<T>& y )
{
  if ( !x && !!y )
  {
    x.reset(std::move(*y));
    y.reset();
  }
  else if ( !!x && !y )
  {
    y.reset(std::move(*x));
    x.reset();
  }
  else if ( !!x && !!y )
  {
// GCC > 3.2 and all other compilers have the using declaration at function scope (FLC)
#ifndef BOOST_OPTIONAL_STD_SWAP_INTRODUCED_AT_NS_SCOPE
    // allow for Koenig lookup
    using std::swap ;
#endif
    swap(*x,*y);
  }
}

Attachments (2)

14-optional-partial-rvalue.patch (6.2 KB ) - added by mlcreech@… 12 years ago.
Partial move support
14-optional-partial-rvalue.2.patch (6.2 KB ) - added by mlcreech@… 11 years ago.
Updated patch for 1.48

Download all attachments as: .zip

Change History (17)

comment:1 by mlcreech@…, 12 years ago

Are there any plans to add support for rvalue references to boost::optional? This makes a very big difference on C++0x code.

Also, bug severity should not be "optimization" - as things are, classes which are movable but not copyable can't be used at all with boost::optional (compile errors), whereas if move semantics were respected it would work fine.

by mlcreech@…, 12 years ago

Partial move support

comment:2 by mlcreech@…, 12 years ago

I've attached an example patch which fixes my build problems when using boost::optional with noncopyable objects. It doesn't handle move semantics for boost::optional objects themselves, but does for the underlying type.

comment:3 by NN, 12 years ago

I support this patch. C++0x move feature makes code much better optimized !

comment:4 by Fernando Cacciola, 12 years ago

I will be working on adding this next week or the one after.

Best

comment:5 by boost@…, 11 years ago

Hi, I stumbled across this ticket while looking for a solution for the same issues with move-only types in Boost.Optional, will there be a fix anytime near in the future?

Regards, Arne

comment:6 by mlcreech@…, 11 years ago

Severity: OptimizationProblem
Type: Feature RequestsPatches
Version: Boost 1.35.0Boost 1.38.0

Arne, does this patch work for you? It at least fixed the build problems I was having. I'm still using it with 1.48, I'll post an updated version.

I'm going to change the type/severity also: this problem prevents code from building which should otherwise work when move semantics are enabled.

by mlcreech@…, 11 years ago

Updated patch for 1.48

comment:7 by Fernando Cacciola, 11 years ago

Status: newassigned

I started work on this

comment:8 by jmckesson@…, 10 years ago

Version: Boost 1.38.0Boost 1.50.0

Is this going to be implemented anytime soon?

comment:9 by tjackson@…, 10 years ago

I'd love to see this completed. Any updates?

comment:10 by zeratul976@…, 10 years ago

+1, I'd like to see this as well!

comment:11 by Fernando Cacciola, 10 years ago

I (re)-started working on it this week.

comment:12 by Mozza314, 10 years ago

+1 Surely this is a no-brainer. optional should take advantage of move semantics.

comment:13 by Lindley, 9 years ago

This still appears to be unfixed in 1.54. This needs to get done.

As it stands now, this bug prevents the Signals2 library from being used with a move-only return value from a slot such as a std::future, among other things.

comment:14 by akrzemi1, 8 years ago

Milestone: Boost 1.36.0Boost 1.56.0
Owner: changed from Fernando Cacciola to akrzemi1
Status: assignednew
Version: Boost 1.50.0Boost 1.56.0

comment:15 by akrzemi1, 8 years ago

Resolution: fixed
Status: newclosed
Version: Boost 1.56.0Boost 1.36.0

move semantics, improved swap and function emplace() added in branch master.

Note: See TracTickets for help on using tickets.