Skip to content

Commit 86bc7a1

Browse files
authored
[SYCL] Add copy/move ctors and assignment operators to sycl::detail::optional (intel#19834)
Currently, those are implicitly defined and perform bitwise copy of Storage, that is incorrect if T has non-trivial copy ctors etc. ___ cherry-pick of: intel@ffc2512 Patch-by: Alexandr-Konovalov <[email protected]>
1 parent 44d3bc2 commit 86bc7a1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

sycl/include/sycl/detail/optional.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ template <typename T> class optional {
2222
public:
2323
constexpr optional() noexcept {}
2424
constexpr optional(std::nullopt_t) noexcept : optional() {}
25+
constexpr optional(const optional &Other)
26+
: ContainsValue{Other.ContainsValue} {
27+
if (Other.ContainsValue)
28+
new (Storage) T(Other.value());
29+
}
30+
constexpr optional(optional &&Other) : ContainsValue{Other.ContainsValue} {
31+
new (Storage) T(std::move(Other.value()));
32+
Other.ContainsValue = false;
33+
}
2534

2635
template <typename U>
2736
constexpr optional(const optional<U> &Other)
@@ -60,6 +69,21 @@ template <typename T> class optional {
6069
return *this;
6170
}
6271

72+
optional &operator=(const optional<T> &Other) {
73+
if (has_value())
74+
reinterpret_cast<T *>(Storage)->~T();
75+
ContainsValue = Other.has_value();
76+
new (Storage) T(Other.value());
77+
return *this;
78+
}
79+
optional &operator=(optional<T> &&Other) noexcept {
80+
if (has_value())
81+
reinterpret_cast<T *>(Storage)->~T();
82+
ContainsValue = Other.has_value();
83+
new (Storage) T(std::move(Other.value()));
84+
return *this;
85+
}
86+
6387
template <typename U> optional &operator=(const optional<U> &Other) {
6488
if (has_value())
6589
reinterpret_cast<T *>(Storage)->~T();

0 commit comments

Comments
 (0)