From fe138be6948af598f77172ff46834d05b0cb8ac3 Mon Sep 17 00:00:00 2001 From: Alexandr-Konovalov Date: Thu, 14 Aug 2025 17:07:52 +0200 Subject: [PATCH] [SYCL] Add copy/move ctors and assignment operators to sycl::detail::optional (#19775) Currently, those are implicitly defined and perform bitwise copy of Storage, that is incorrect if T has non-trivial copy ctors etc. --- sycl/include/sycl/detail/optional.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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();