Skip to content

Commit

Permalink
Add vec explicit conversion
Browse files Browse the repository at this point in the history
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<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 Dec 3, 2024
1 parent 94eb558 commit 0e3a33e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions adoc/chapters/programming_interface.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
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<T>#, and
* [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 +18676,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 0e3a33e

Please sign in to comment.