diff --git a/sycl/include/sycl/detail/optional.hpp b/sycl/include/sycl/detail/optional.hpp index ff14fabe82007..cfe4723367ce9 100644 --- a/sycl/include/sycl/detail/optional.hpp +++ b/sycl/include/sycl/detail/optional.hpp @@ -22,6 +22,15 @@ template class optional { public: constexpr optional() noexcept {} constexpr optional(std::nullopt_t) noexcept : optional() {} + constexpr optional(const optional &Other) + : ContainsValue{Other.ContainsValue} { + if (Other.ContainsValue) + new (Storage) T(Other.value()); + } + constexpr optional(optional &&Other) : ContainsValue{Other.ContainsValue} { + new (Storage) T(std::move(Other.value())); + Other.ContainsValue = false; + } template constexpr optional(const optional &Other) @@ -60,6 +69,21 @@ template class optional { return *this; } + optional &operator=(const optional &Other) { + if (has_value()) + reinterpret_cast(Storage)->~T(); + ContainsValue = Other.has_value(); + new (Storage) T(Other.value()); + return *this; + } + optional &operator=(optional &&Other) noexcept { + if (has_value()) + reinterpret_cast(Storage)->~T(); + ContainsValue = Other.has_value(); + new (Storage) T(std::move(Other.value())); + return *this; + } + template optional &operator=(const optional &Other) { if (has_value()) reinterpret_cast(Storage)->~T();