| Home | Libraries | People | FAQ | More

#### Detailed Semantics - Optional References

> `optional<T&>::optional() noexcept;`

> `optional<T&>::optional(none_t) noexcept;`

- Postconditions: `bool(*this) == false`; `*this` refers to nothing.

> `template<class R> optional<T&>::optional(R&& r) noexcept;`

- Postconditions: `bool(*this) == true`; `addressof(**this) == addressof(r)`.
- Remarks: Unless `R` is an lvalue reference, the program is ill-formed. This constructor does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
- Notes: This constructor is declared `explicit` on compilers that do not correctly support binding to const lvalues of integral types. For more details see here.
- Example:

      T v;
      T& vref = v ;
      optional<T&> opt(vref);
      assert ( *opt == v ) ;
      ++ v ; // mutate referee
      assert (*opt == v);

> `template<class R> optional<T&>::optional(bool cond, R&& r) noexcept;`

- Effects: Initializes `ref` with expression `cond ? addressof(r) : nullptr`.
- Postconditions: `bool(*this) == cond`; If `bool(*this)`, `addressof(**this) == addressof(r)`.
- Remarks: Unless `R` is an lvalue reference, the program is ill-formed. This constructor does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.

> `optional<T&>::optional ( optional const& rhs ) noexcept ;`

- Effects: Initializes `ref` with expression `rhs.ref`.
- Postconditions: `bool(*this) == bool(rhs)`.
- Example:

      optional<T&> uninit ;
      assert (!uninit);

      optional<T&> uinit2 ( uninit ) ;
      assert ( uninit2 == uninit );

      T v = 2 ; T& ref = v ;
      optional<T> init(ref);
      assert ( *init == v ) ;

      optional<T> init2 ( init ) ;
      assert ( *init2 == v ) ;

      v = 3 ;

      assert ( *init  == 3 ) ;
      assert ( *init2 == 3 ) ;

> `template<class U> explicit optional<T&>::optional ( optional<U&> const& rhs ) noexcept ;`

- Requires: `is_convertible<U&, T&>::value` is `true`.
- Effects: Initializes `ref` with expression `rhs.ref`.
- Postconditions: `bool(*this) == bool(rhs)`.

> `optional<T&>::operator= ( none_t ) noexcept ;`

- Effects: Assigns `ref` with expression `nullptr`.
- returns: `*this`.
- Postconditions: `bool(*this) == false`.

> `optional& optional<T&>::operator= ( optional const& rhs ) noexcept ;`

- Effects: Assigns `ref` with expression `rhs.ref`.
- returns: `*this`.
- Postconditions: `bool(*this) == bool(rhs)`.
- Notes: This behaviour is called rebinding semantics. See here for details.
- Example:

      int a = 1 ;
      int b = 2 ;
      T& ra = a ;
      T& rb = b ;
      optional<int&> def ;
      optional<int&> ora(ra) ;
      optional<int&> orb(rb) ;

      def = orb ; // binds 'def' to 'b' through 'rb' wrapped within 'orb'
      assert ( *def == b ) ;
      *def = ora ; // changes the value of 'b' to a copy of the value of 'a'
      assert ( b == a ) ;
      int c = 3;
      int& rc = c ;
      optional<int&> orc(rc) ;
      ora = orc ; // REBINDS ora to 'c' through 'rc'
      c = 4 ;
      assert ( *ora == 4 ) ;

> `template<class U> optional& optional<T&>::operator= ( optional<U&> const& rhs ) noexcept ;`

- Requires: `is_convertible<U&, T&>::value` is `true`.
- Effects: Assigns `ref` with expression `rhs.ref`.
- returns: `*this`.
- Postconditions: `bool(*this) == bool(rhs)`.

> `template<class R> optional& optional<T&>::operator= ( R&& r ) noexcept ;`

- Effects: Assigns `ref` with expression `r`.
- returns: `*this`.
- Postconditions: `bool(*this) == true`.
- Remarks: Unless `R` is an lvalue reference, the program is ill-formed. This function does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.
- Example:

      int a = 1 ;
      int b = 2 ;
      T& ra = a ;
      T& rb = b ;
      optional<int&> def ;
      optional<int&> opt(ra) ;

      def = rb ; // binds 'def' to 'b' through 'rb'
      assert ( *def == b ) ;
      *def = a ; // changes the value of 'b' to a copy of the value of 'a'
      assert ( b == a ) ;
      int c = 3;
      int& rc = c ;
      opt = rc ; // REBINDS to 'c' through 'rc'
      c = 4 ;
      assert ( *opt == 4 ) ;

> `void optional<T&>::emplace( R&& r ) noexcept ;`

- Effects: Assigns `ref` with expression `r`.
- Postconditions: `bool(*this) == true`.
- Remarks: Unless `R` is an lvalue reference, the program is ill-formed. This function does not participate in overload resolution if `decay<R>` is an instance of `boost::optional`.

> `T& optional<T&>::get() const ;`

> `T& optional<T&>::operator *() const ;`

- Requires: `bool(*this) == true`.
- Effects: Returns `*ref`.
- Throws: Nothing.
- Example:

      T v ;
      T& vref = v ;
      optional<T&> opt ( vref );
      T const& vref2 = *opt;
      assert ( vref2 == v ) ;
      ++ v ;
      assert ( *opt == v ) ;

> `T* optional<T&>::operator -> () const ;`

- Requires: `bool(*this) == true`.
- Effects: Returns `ref`.
- Throws: Nothing.

> `T& optional<T&>::value() const ;`

- Effects: Equivalent to `return bool(*this) ? *val : throw bad_optional_access();`.

> `template<class R> T& optional<T&>::value_or( R&& r ) const noexcept;`

- Effects: Equivalent to `if (*this) return **this; else return r;`.
- Remarks: Unless `R` is an lvalue reference, the program is ill-formed.

> `template<class F> T& optional<T&>::value_or( F f ) const ;`

- Effects: Equivalent to `if (*this) return **this; else return f();`.
- Remarks: Unless `decltype(f())` is an lvalue reference, the program is ill-formed.

> `template<class F> auto optional<T&>::map( F f ) const ->` see below`;`

- Effects: Equivalent to `if (*this) return f(**this); else return none;`.
- Remarks: The return type of this function is `optional<decltype(f(**this))>`.

> `template<class F> auto optional<T&>::flat_map( F f ) const ->` see below`;`

- Requires: The return type of expression `f(**this)` is `optional<U>` for some object or reference type `U`.
- Effects: Equivalent to `if (*this) return f(**this); else return none;`.
- Remarks: The return type of this function is `optional<U>`.

> `T* optional<T&>::get_ptr () const noexcept;`

- Returns: `ref`.

> `bool has_value() const noexcept;`

> `optional<T&>::operator bool () const noexcept;`

- Returns: `bool(ref)`.

> `optional<T&>::operator ! () const noexcept;`

- Returns: `!bool(ref)`.

> `void optional<T&>::reset() noexcept;`

- Effects: Same as `*this = none`.

> `template<class R> void optional<T&>::reset ( R&& r) noexcept;`

- Effects: Equivalent to `*this = std::forward<R>(r)`.
- Remarks: This function is deprecated.

> `bool optional<T&>::is_initialized() const noexcept;`

- Effects: Equivalent to `return bool(*this)`.
- Remarks: This function is deprecated.

> `template<class R> T& optional<T&>::get_value_or( R&& r ) const noexcept;`

- Effects: Equivalent to `return value_or(std::forward<R>(r);`.
- Remarks: This function is deprecated.