Skip to content

[SYCL] Add copy/move ctors and assignment operators to sycl::detail::… #19834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions sycl/include/sycl/detail/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ template <typename T> 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 <typename U>
constexpr optional(const optional<U> &Other)
Expand Down Expand Up @@ -60,6 +69,21 @@ template <typename T> class optional {
return *this;
}

optional &operator=(const optional<T> &Other) {
if (has_value())
reinterpret_cast<T *>(Storage)->~T();
ContainsValue = Other.has_value();
new (Storage) T(Other.value());
return *this;
}
optional &operator=(optional<T> &&Other) noexcept {
if (has_value())
reinterpret_cast<T *>(Storage)->~T();
ContainsValue = Other.has_value();
new (Storage) T(std::move(Other.value()));
return *this;
}

template <typename U> optional &operator=(const optional<U> &Other) {
if (has_value())
reinterpret_cast<T *>(Storage)->~T();
Expand Down