Skip to content

Commit

Permalink
Add "vec" explicit conversion
Browse files Browse the repository at this point in the history
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<sycl::half, 1> h1;
int i = static_cast<int>(h1);
if (h1) {}
```

These changes correspond to slides 8 - 9 of the presentation that was
discussed in the WG meetings.
  • Loading branch information
gmlueck committed Nov 26, 2024
1 parent 94eb558 commit 2797abd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions adoc/chapters/programming_interface.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
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<T>#.
The type [code]#T# is not [code]#DataT#.

_Returns:_ The value of the vector's element converted to [code]#T#.

a@
[source]
----
Expand Down Expand Up @@ -18661,6 +18674,10 @@ operator vector_t() const
#endif

operator DataT() const

template<typename T>
explicit operator T() const

static constexpr size_t byte_size() noexcept
static constexpr size_t size() noexcept

Expand Down
4 changes: 4 additions & 0 deletions adoc/headers/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ template <typename DataT, int NumElements> class vec {
// Available only when: NumElements == 1
operator DataT() const;

// Available only when: NumElements == 1
template<typename T>
explicit operator T() const;

static constexpr size_t byte_size() noexcept;

static constexpr size_t size() noexcept;
Expand Down

0 comments on commit 2797abd

Please sign in to comment.