Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is the reference returned by operator[] to restrictive? #43

Open
krzikalla opened this issue Nov 14, 2023 · 1 comment
Open

Is the reference returned by operator[] to restrictive? #43

krzikalla opened this issue Nov 14, 2023 · 1 comment

Comments

@krzikalla
Copy link

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?

@mattkretz
Copy link
Member

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants