VcDevel / std-simd

std::experimental::simd for GCC [ISO/IEC TS 19570:2018]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is the reference returned by operator[] to restrictive?

krzikalla opened this issue · comments

By disallowing a binding of simd<>::reference to an lvalue the code below (ref. https://godbolt.org/z/7qW8bffe5) becomes clumsy:

#include <iostream>
#include <experimental/simd>

namespace stdx = std::experimental;

void foo(auto&& x)
{
    std::forward<decltype(x)>(x) = .2;
    //x = .3;
}

int main()
{
  stdx::simd<double> x = 0;
  x[0] = .1;
  foo(x[1]);
  for (int i = 0; i < x.size(); ++i) std::cout << x[i] << ' ';
}

The x = .3 clause doesn't compile, since the assignment is declared as reference::operator=(...) && and x is an lvalue. Why is the operator= restricted to rvalues?
Is it known, that the above code becomes clumsy by this restriction?

This is intentional. A simd::reference easily becomes a dangling reference and there's no language support to make the reference proxy "safer" (there should be). Therefore the reference type tries as hard as it can to either be assigned to directly or turn into a prvalue of the simd::value_type. You should not turn it into an lvalue. If you do, it can only be converted to simd::value_type nothing else.
The line foo(x[1]) should call foo<double>(double&&) rather than deducing a simd::reference. C++ can't do that...