Boost logo

Boost :

Subject: [boost] [optional] get() misses optional r-value overload in contrast to operator* and value()
From: Viktor Sehr (viktor.sehr_at_[hidden])
Date: 2018-03-23 01:04:26


Both *value()* and *operator** has an overload to handle if the
*optional<T>* object is an r-value. However *get()* does not have the
r-value overload.

This leads to undefined behavior using *get()*, but not with *operator**
 and *value()*, in the following example:

auto func0() {
  return optional<int>{3};
}
auto func1() {
  const auto& x = func0().get(); // Undefined behavior
  const auto& y = func0().value(); // Correct
  const auto& z = *func0(); // Correct
}

Is this intended, I can't see any reason why the r-value overload of .get()
would be missing?
Otherwise, the following overload of .get() should be added:

reference_type_of_temporary_wrapper get() && {
  BOOST_ASSERT(this->is_initialized());
  return boost::move(this->get_impl());
}

/Viktor Sehr, Software Developer at Toppluva


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