Commit 75532d4
committed
optional::transform: ensure that it works with non-movable payloads
This is supposed to work and it's explicitly spelled out in the standard
text.
If the function called by transform returns a non-movable prvalue,
we cannot rely on optional(T&&) constructor for this, because that will
not simply materialize it into the payload of the returned optional.
Instead, add a new private constructor for optional with a private tag.
Inside that constructor, we invoke the function and use it to initialize
the payload; this will not require any moves due to guaranteed copy
elision. In transform(), we call that constructor, passing the function
and the value stored in *this to it.
A similar change is needed for the optional<T&>.
Since we need to call this private optional<U>'s constructor from a
generic optional<T>::transform, we need to grant friendship to all
possible optional specializations.1 parent 1d93a02 commit 75532d4
File tree
3 files changed
+67
-5
lines changed- include/beman/optional
- tests/beman/optional
3 files changed
+67
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
52 | 58 | | |
53 | 59 | | |
54 | 60 | | |
| |||
403 | 409 | | |
404 | 410 | | |
405 | 411 | | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
406 | 419 | | |
407 | 420 | | |
408 | 421 | | |
| |||
785 | 798 | | |
786 | 799 | | |
787 | 800 | | |
788 | | - | |
| 801 | + | |
789 | 802 | | |
790 | 803 | | |
791 | 804 | | |
| |||
796 | 809 | | |
797 | 810 | | |
798 | 811 | | |
799 | | - | |
| 812 | + | |
800 | 813 | | |
801 | 814 | | |
802 | 815 | | |
| |||
807 | 820 | | |
808 | 821 | | |
809 | 822 | | |
810 | | - | |
| 823 | + | |
811 | 824 | | |
812 | 825 | | |
813 | 826 | | |
| |||
818 | 831 | | |
819 | 832 | | |
820 | 833 | | |
821 | | - | |
| 834 | + | |
822 | 835 | | |
823 | 836 | | |
824 | 837 | | |
| |||
1207 | 1220 | | |
1208 | 1221 | | |
1209 | 1222 | | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
1210 | 1231 | | |
1211 | 1232 | | |
1212 | 1233 | | |
| |||
1356 | 1377 | | |
1357 | 1378 | | |
1358 | 1379 | | |
1359 | | - | |
| 1380 | + | |
1360 | 1381 | | |
1361 | 1382 | | |
1362 | 1383 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| |||
81 | 83 | | |
82 | 84 | | |
83 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
84 | 104 | | |
85 | 105 | | |
86 | 106 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| |||
89 | 91 | | |
90 | 92 | | |
91 | 93 | | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
92 | 113 | | |
93 | 114 | | |
94 | 115 | | |
| |||
0 commit comments