|
| 1 | + |
| 2 | +[section Optional references] |
| 3 | + |
| 4 | +This library allows the template parameter `T` to be of reference type: |
| 5 | +`T&`, and to some extent, `T const&`. |
| 6 | + |
| 7 | +However, since references are not real objects some restrictions apply and |
| 8 | +some operations are not available in this case: |
| 9 | + |
| 10 | +* Converting constructors |
| 11 | +* Converting assignment |
| 12 | +* InPlace construction |
| 13 | +* InPlace assignment |
| 14 | +* Value-access via pointer |
| 15 | + |
| 16 | +Also, even though `optional<T&>` treats it wrapped pseudo-object much as |
| 17 | +a real value, a true real reference is stored so aliasing will ocurr: |
| 18 | + |
| 19 | +* Copies of `optional<T&>` will copy the references but all these references |
| 20 | +will nonetheless refer to the same object. |
| 21 | +* Value-access will actually provide access to the referenced object |
| 22 | +rather than the reference itself. |
| 23 | + |
| 24 | +[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 [link boost_optional.dependencies_and_portability.optional_reference_binding Dependencies and Portability section].] |
| 25 | + |
| 26 | +[heading Rvalue references] |
| 27 | + |
| 28 | +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. |
| 29 | + |
| 30 | + const int& i = 1; // legal |
| 31 | + optional<const int&> oi = 1; // illegal |
| 32 | + |
| 33 | +[endsect] |
| 34 | + |
| 35 | +[section Rebinding semantics for assignment of optional references] |
| 36 | + |
| 37 | +If you assign to an ['uninitialized ] `optional<T&>` the effect is to bind (for |
| 38 | +the first time) to the object. Clearly, there is no other choice. |
| 39 | + |
| 40 | + int x = 1 ; |
| 41 | + int& rx = x ; |
| 42 | + optional<int&> ora ; |
| 43 | + optional<int&> orb(x) ; |
| 44 | + ora = orb ; // now 'ora' is bound to 'x' through 'rx' |
| 45 | + *ora = 2 ; // Changes value of 'x' through 'ora' |
| 46 | + assert(x==2); |
| 47 | + |
| 48 | +If you assign to a bare C++ reference, the assignment is forwarded to the |
| 49 | +referenced object; its value changes but the reference is never rebound. |
| 50 | + |
| 51 | + int a = 1 ; |
| 52 | + int& ra = a ; |
| 53 | + int b = 2 ; |
| 54 | + int& rb = b ; |
| 55 | + ra = rb ; // Changes the value of 'a' to 'b' |
| 56 | + assert(a==b); |
| 57 | + b = 3 ; |
| 58 | + assert(ra!=b); // 'ra' is not rebound to 'b' |
| 59 | + |
| 60 | +Now, if you assign to an ['initialized ] `optional<T&>`, the effect is to |
| 61 | +[*rebind] to the new object instead of assigning the referee. This is unlike |
| 62 | +bare C++ references. |
| 63 | + |
| 64 | + int a = 1 ; |
| 65 | + int b = 2 ; |
| 66 | + int& ra = a ; |
| 67 | + int& rb = b ; |
| 68 | + optional<int&> ora(ra) ; |
| 69 | + optional<int&> orb(rb) ; |
| 70 | + ora = orb ; // 'ora' is rebound to 'b' |
| 71 | + *ora = 3 ; // Changes value of 'b' (not 'a') |
| 72 | + assert(a==1); |
| 73 | + assert(b==3); |
| 74 | + |
| 75 | +[heading Rationale] |
| 76 | + |
| 77 | +Rebinding semantics for the assignment of ['initialized ] `optional` references has |
| 78 | +been chosen to provide [*consistency among initialization states] even at the |
| 79 | +expense of lack of consistency with the semantics of bare C++ references. |
| 80 | +It is true that `optional<U>` strives to behave as much as possible as `U` |
| 81 | +does whenever it is initialized; but in the case when `U` is `T&`, doing so would |
| 82 | +result in inconsistent behavior w.r.t to the lvalue initialization state. |
| 83 | + |
| 84 | +Imagine `optional<T&>` forwarding assignment to the referenced object (thus |
| 85 | +changing the referenced object value but not rebinding), and consider the |
| 86 | +following code: |
| 87 | + |
| 88 | + optional<int&> a = get(); |
| 89 | + int x = 1 ; |
| 90 | + int& rx = x ; |
| 91 | + optional<int&> b(rx); |
| 92 | + a = b ; |
| 93 | + |
| 94 | +What does the assignment do? |
| 95 | + |
| 96 | +If `a` is ['uninitialized], the answer is clear: it binds to `x` (we now have |
| 97 | +another reference to `x`). |
| 98 | +But what if `a` is already ['initialized]? it would change the value of the |
| 99 | +referenced object (whatever that is); which is inconsistent with the other |
| 100 | +possible case. |
| 101 | + |
| 102 | +If `optional<T&>` would assign just like `T&` does, you would never be able to |
| 103 | +use Optional's assignment without explicitly handling the previous |
| 104 | +initialization state unless your code is capable of functioning whether |
| 105 | +after the assignment, `a` aliases the same object as `b` or not. |
| 106 | + |
| 107 | +That is, you would have to discriminate in order to be consistent. |
| 108 | + |
| 109 | +If in your code rebinding to another object is not an option, then it is very |
| 110 | +likely that binding for the first time isn't either. In such case, assignment |
| 111 | +to an ['uninitialized ] `optional<T&>` shall be prohibited. It is quite possible |
| 112 | +that in such a scenario it is a precondition that the lvalue must be already |
| 113 | +initialized. If it isn't, then binding for the first time is OK |
| 114 | +while rebinding is not which is IMO very unlikely. |
| 115 | +In such a scenario, you can assign the value itself directly, as in: |
| 116 | + |
| 117 | + assert(!!opt); |
| 118 | + *opt=value; |
| 119 | + |
| 120 | +[endsect] |
0 commit comments