Opened 5 years ago

Last modified 5 years ago

#13137 new Bugs

boost::optional::operator*() doesn't have "const &&" overload, allowing incorrect usage

Reported by: for.gcc.bugzilla@… Owned by: Fernando Cacciola
Milestone: To Be Determined Component: optional
Version: Boost 1.65.0 Severity: Problem
Keywords: Cc:

Description

#include <boost/optional.hpp>

const boost::optional<int> f()
{
    return {1};
}

int main()
{
    const int& ref = *f();
    // creates a boost::optional temporary
    // "*tmp" binds to "optional::operator*() const&"
    // "ref" starts pointing to internals of the returned temporary
    // temporary gets destroyed
    // we have a dangling reference
}

at the same time, std::optional in C++17 does have

constexpr const T&& operator*() const &&;

overload, which lets the code above work correctly.

Change History (2)

comment:1 by boost bugs <for.gcc.bugzilla@…>, 5 years ago

the same lack of "() const&&" overload with boost::optional::value()

comment:2 by boost bugs <for.gcc.bugzilla@…>, 5 years ago

Sorry, I just realised that even if boost::optional::operator*() had such an overload, it still wouldn't fix the problem in the bug description.

Note: See TracTickets for help on using tickets.