Skip to content

Commit e6c9e18

Browse files
SnektronNB4444
authored andcommitted
Fix thrust::optional<T&>::emplace()
Where optional<T> inherits optional<T>::construct via a series of classes, optional<T&> does not. This means that optional<T&>::emplace() was broken and called into a member function that did not exist. This replaces the functionality to make optional<T&>::emplace() change the stored reference to the new one. Note that it does _not_ emplace the referee, as this would lead to questionable behavior when the optional holds nullopt. This was revealed by a change in LLVM, see llvm/llvm-project#90152 and ROCm#404.
1 parent 256fd73 commit e6c9e18

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <thrust/optional.h>
2+
3+
#include <unittest/unittest.h>
4+
5+
int main()
6+
{
7+
{
8+
int a = 10;
9+
10+
thrust::optional<int&> maybe(a);
11+
12+
int b = 20;
13+
maybe.emplace(b);
14+
15+
ASSERT_EQUAL(maybe.value(), 20);
16+
// Emplacing with b shouldn't change a
17+
ASSERT_EQUAL(a, 10);
18+
19+
int c = 30;
20+
maybe.emplace(c);
21+
22+
ASSERT_EQUAL(maybe.value(), 30);
23+
ASSERT_EQUAL(b, 20);
24+
}
25+
26+
{
27+
thrust::optional<int&> maybe;
28+
29+
int b = 21;
30+
maybe.emplace(b);
31+
32+
ASSERT_EQUAL(maybe.value(), 21);
33+
34+
int c = 31;
35+
maybe.emplace(c);
36+
37+
ASSERT_EQUAL(maybe.value(), 31);
38+
ASSERT_EQUAL(b, 21);
39+
}
40+
}

0 commit comments

Comments
 (0)