std::experi...::operator[](3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::experi...::operator[](3)

okay so bestie, welcome to the C++ Standard Library corner of the manual β€” section 3, no less β€” and today's main character is std::experimental::simd::operator[]. yes the title's got that little experi... ellipsis thing going on, that's just how it's filed, don't come for her πŸŽ€


✨ NAME ✨

std::experimental::simd::operator[] β€” literally std::experimental::simd::operator[], that's the whole name, we love a self-titled icon πŸ‘‘


πŸ¦‹ Synopsis πŸ¦‹

reference operator[]( std::size_t i );        (1) (parallelism TS v2)
value_type operator[]( std::size_t i ) const; (2) (parallelism TS v2)

so these subscript operators are the whole vibe β€” they let you read and write single elements of a simd. like, one element at a time, slay 🫢

  • (1) β€” returns a reference proxy to the i-th element. and listen, this is important: this proxy type should not be captured into an lvalue. lvalues of simd::reference can only convert to value_type. meanwhile rvalues of simd::reference overload assignment and all compound assignment operators as well as swap. iykyk πŸ’…
  • (2) β€” returns a prvalue of the i-th element. and here's the tea: in contrast to containers, which contain objects of type value_type, a simd is not a container of individual objects, and therefore cannot return an lvalue-reference. it's giving "different genre of data structure entirely" and honestly? respect ✨

🌸 Parameters 🌸

  • i β€” the element index. required to be less than size(). that's the rule, don't push it πŸ”₯

πŸ’– Example πŸ’–

// Run this code

#include <cstddef>
#include <cstdint>
#include <experimental/simd>
#include <iostream>
namespace stdx = std::experimental;

int main()
{
    const stdx::native_simd<std::int64_t> a = 3;
    for (std::size_t i = 0; i != a.size(); ++i)
        std::cout << a[i] << ' ';
    std::cout << '\n';
}

⭐ Possible output: ⭐

3 3 3 3 3 3 3 3

eight threes. she ate and left no crumbs πŸ˜­πŸ’€


small reminder bestie: i has to be less than size(), and please don't capture that reference proxy into an lvalue. i'm not being dramatic, the standard said it, not me πŸ’œ

and that's the page!! source: it's from http://cppreference.com, dated 2024.06.10, and it lives at std::experi...::operator[](3) β€” go forth and index your simd elements responsibly, mwah πŸ§šπŸ’–πŸŽ€

πŸ“¦ where this page comes from