std::experi...::operator[](3)
π 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 ofsimd::referencecan only convert tovalue_type. meanwhile rvalues ofsimd::referenceoverload assignment and all compound assignment operators as well asswap. iykyk π(2)β returns a prvalue of the i-th element. and here's the tea: in contrast to containers, which contain objects of typevalue_type, asimdis 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 thansize(). 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:ihas to be less thansize(), 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms