Skip to content

Cannot return non_const reference to contained value for uninitialized optional variable type. #111

Open

Description

@robertramey

I'm having an issue with the following code:

From boost/optional.hpp - lines 1267-1281 (problem lines marked with ~)

// Returns a reference to the value if this is initialized, otherwise,
// the behaviour is UNDEFINED
// No-throw
reference_const_type get() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }
~ reference_type get() { BOOST_ASSERT(this->is_initialized()) ; return this->get_impl(); }

/// Returns a copy of the value if this is initialized, 'v' otherwise
reference_const_type get_value_or ( reference_const_type v ) const { return this->is_initialized() ? get() : v ; }
reference_type get_value_or ( reference_type v ) { return this->is_initialized() ? get() : v ; }

/// Returns a pointer to the value if this is initialized, otherwise,
// the behaviour is UNDEFINED
// No-throw
pointer_const_type operator->() const { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }
~ pointer_type operator->() { BOOST_ASSERT(this->is_initialized()) ; return this->get_ptr_impl() ; }

The code returns non-const reference and pointer. These used for getting a reference to the contained variable holding the type presumably so it can be modified. (this is my case). BUT, the BOOST_ASSERT traps if the variable isn't initialized. This makes it impossible to modify the contained value. The is necessary in the context of the serialization library.

Activity

  1. akrzemi1 commented on Dec 29, 2023

    @akrzemi1
    Member

    Can you show a small program that uses optional and suffers from this problem?

  2. akrzemi1 commented on Feb 18, 2026

    @akrzemi1
    Member

    The use case you describe is intentionally not supported. It is an incorrect usage of this library to try to get a reference to a non-existing contained value.

    Instead, I would recommend first creating the contained value using T's default constructor, and then getting a reference to it:

    optional<int> oi;  // no contained value
    oi.emplace();      // now containing a default-initialized value
    fill_as_you_please(*oi);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions