| Home | Libraries | People | FAQ | More

## Optional references

This library allows the template parameter `T` to be of reference type: `T&`, and to some extent, `T const&`.

However, since references are not real objects some restrictions apply and some operations are not available in this case:

- Converting constructors
- Converting assignment
- InPlace construction
- InPlace assignment
- Value-access via pointer

Also, even though `optional<T&>` treats it wrapped pseudo-object much as a real value, a true real reference is stored so aliasing will ocurr:

- Copies of `optional<T&>` will copy the references but all these references will nonetheless refer to the same object.
- Value-access will actually provide access to the referenced object rather than the reference itself.

| Warning
On compilers that do not conform to Standard C++ rules of reference binding, operations on optional references might give adverse results: rather than binding a reference to a designated object they may create an unexpected temporary and bind to it. For more details see Dependencies and Portability section.

#### Rvalue references

Rvalue references and lvalue references to const have the ability in C++ to extend the life time of a temporary they bind to. Optional references do not have this capability, therefore to avoid surprising effects it is not possible to initialize an optional references from a temporary. Optional rvalue references are disabled altogether. Also, the initialization and assignment of an optional reference to const from rvalue reference is disabled.

    const int& i = 1;            // legal
    optional<const int&> oi = 1; // illegal