From 2797abd94d093231218d70e54860fc39626a8566 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 the second of several changes 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 | 17 +++++++++++++++++ adoc/headers/vec.h | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/adoc/chapters/programming_interface.adoc b/adoc/chapters/programming_interface.adoc index 5f2b2f9c5..ede2b6b41 100644 --- a/adoc/chapters/programming_interface.adoc +++ b/adoc/chapters/programming_interface.adoc @@ -17869,6 +17869,19 @@ 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. +The type [code]#DataT# can be explicitly converted to [code]#T# via +[code]#static_cast#. +The type [code]#T# is not [code]#DataT#. + +_Returns:_ The value of the vector's element converted to [code]#T#. + a@ [source] ---- @@ -18661,6 +18674,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;