From 0e3a33e7bf02cbebdf33c678864558cf7161365a Mon Sep 17 00:00:00 2001 From: Greg Lueck Date: Mon, 25 Nov 2024 17:08:05 -0500 Subject: [PATCH] Add vec explicit conversion This is change 2 of 9 that fix problems with the specification of the `vec` class. An implementation that follows the existing specification would not accept common code patterns and would not pass the CTS. None of the existing implementations actually follow the existing specification. This change adds an explicit conversion when the `vec` has one element, allowing a conversion to `T` whenever `DataT` can be explicitly converted to `T`. This enables code like the following, where the element type `DataT` is itself a class: ``` sycl::vec h1; int i = static_cast(h1); if (h1) {} ``` These changes correspond to slides 8 - 9 of the presentation that was discussed in the WG meetings. --- adoc/chapters/programming_interface.adoc | 19 +++++++++++++++++++ adoc/headers/vec.h | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/adoc/chapters/programming_interface.adoc b/adoc/chapters/programming_interface.adoc index 5f2b2f9c5..bf3f9d774 100644 --- a/adoc/chapters/programming_interface.adoc +++ b/adoc/chapters/programming_interface.adoc @@ -17869,6 +17869,21 @@ to which [code]#DataT# is implicitly convertible. Note that conversion operator shall not be templated to allow standard conversion sequence for implicit conversion. +a@ +[source] +---- +template +explicit operator T() const +---- +a@ _Constraints:_ + +* [code]#NumElements# is equal to 1, and +* [code]#DataT# can be explicitly converted to [code]#T# via + [code]#static_cast#, and +* [code]#T# is not [code]#DataT#. + +Returns the value of the vector's element converted to [code]#T#. + a@ [source] ---- @@ -18661,6 +18676,10 @@ operator vector_t() const #endif operator DataT() const + +template +explicit operator T() const + static constexpr size_t byte_size() noexcept static constexpr size_t size() noexcept diff --git a/adoc/headers/vec.h b/adoc/headers/vec.h index 854294e22..6d2ea017a 100644 --- a/adoc/headers/vec.h +++ b/adoc/headers/vec.h @@ -58,6 +58,10 @@ template class vec { // Available only when: NumElements == 1 operator DataT() const; + // Available only when: NumElements == 1 + template + explicit operator T() const; + static constexpr size_t byte_size() noexcept; static constexpr size_t size() noexcept;