Is it possible to move a boost::optional?

user1203803 picture user1203803 · May 28, 2012 · Viewed 7.2k times · Source

I've been trying to define a defaulted move constructor in a class with a boost::optional member variable.

#include <boost/optional.hpp>
#include <utility>
#include <vector>

struct bar {std::vector<int> vec;};

struct foo {
  foo() = default;
  foo(foo&&) = default;
  boost::optional<bar> hello;
};

int main() {
  foo a;
  foo b(std::move(a));
}

My compiler supports both move semantics and defaulted move constructors, but I cannot get this to work.

% clang++ foo.cc -std=c++11 -stdlib=libc++
foo.cc:15:7: error: call to deleted constructor of 'foo'
  foo b(std::move(a));
      ^ ~~~~~~~~~~~~
foo.cc:9:3: note: function has been explicitly marked deleted here
  foo(foo&&) = default;
  ^
1 error generated.

Is there a way to move a boost::optional without modifying Boost's source code? Or should I wait until Boost supports moving?

I had the same problem with boost::any in the past.

Answer

Andrzej picture Andrzej · Sep 22, 2014

Boost.Optional supports move construction only since Boost version 1.56. See the release notes.