KhronosGroup / KhronosGroup/SYCL-Docs
Why does `vec::operator[]` accept an `int` as element index?
- Dominant language
- JavaScript
- Stars
- 158
- Forks
- 80
- Avg merge
- 7d 6h
- Merged PRs (30d)
- 5
Description
### Specification Version
SYCL 2020 (Revision 10)
### Section Number(s)
[4.14.2.1. Vec interface](https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html#_vec_interface)
### Issue Description
`vec::size()` returns `size_t`, but `vec::operator[]` accepts an `int` index. Why is it not `size_t`? Is it an oversite, or was there a reason behind it?
I understand that switching this may break someone's codebase (if they have `-Wconversion -Werror`), but writing a generic code (which targets both `marray` and `vec` at the same time) is pretty annoying under `-Wconversion`, because suddenly you need different index types for these two.
### Code Example (Optional)
```c++
template
static inline auto dot_host_impl(T0 x, T1 y) {
if constexpr (detail::is_scalar_arithmetic::value) {
return x * y;
} else {
auto R = x[0] * y[0];
for (size_t i = 1; i < detail::num_elements::value; ++i)
R += x[i] * y[i];
return R;
}
}
```
`detail::num_elements::value` is of type `size_t`. This function compiles fine with `-Wconversion -Werror` if both `T0` and `T1` are `marray`, but if one of them is `vec`, then `size_t i` can't be used anymore as `operator[]` argument because of implicit narrowing conversion.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with SYCL 2020 section 4.14.2.1, then compare the documented index types for vec and marray using the provided generic example. Done means the specification records a decided rationale or a clearly specified change for vec::operator[] indexing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100