Namespaces
Variants

std::simd::basic_vec<T,Abi>::begin, std::simd::basic_vec<T,Abi>::cbegin

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
 
constexpr iterator begin() noexcept;
(1) (since C++26)
constexpr const_iterator begin() const noexcept;
(2) (since C++26)
constexpr const_iterator cbegin() const noexcept;
(3) (since C++26)

Returns an iterator to the first element of *this.

Return value

An iterator that refers to *this and to its element at index 0. iterator and const_iterator are /*simd-iterator*/<basic_vec> and /*simd-iterator*/<const basic_vec>.

Complexity

Constant.

Notes

Both iterator and const_iterator are read-only: dereferencing either one returns a copy of the element, not a reference, so the elements cannot be modified through an iterator.

The iterator stores a pointer to *this, not a copy of the elements: it observes later changes to *this, and it dangles once *this is destroyed, as in auto it = (a + b).begin();.

Dereferencing an iterator reads one element at a run-time index, as operator[] does: GCC on x86-64 stores the vector register to the stack and loads the element from there. To copy all elements to memory, use std::simd::unchecked_store or std::simd::partial_store, which use vector store instructions.

Example

#include <algorithm>
#include <concepts>
#include <print>
#include <simd>

using V = std::simd::vec<int, 8>;

int main()
{
    V v([](int i) { return i * i - 10; });
    std::println("v = {}", v);

    V::iterator it = v.begin();
    std::println("*it = {}, it[2] = {}", *it, it[2]);
    // *it = 0; // error: dereferencing gives a value, not a reference

    auto pos = std::ranges::find_if(v, [](int x) { return x >= 0; });
    std::println("first non-negative element: v[{}] = {}", pos - v.begin(), *pos);

    static_assert(std::same_as<decltype(v.cbegin()), V::const_iterator>);
    V::const_iterator cit = it; // iterator converts to const_iterator
    v *= 2;
    std::println("after v *= 2: *cit = {}", *cit); // still refers to v
}

Output:

v = [-10, -9, -6, -1, 6, 15, 26, 39]
*it = -10, it[2] = -6
first non-negative element: v[4] = 6
after v *= 2: *cit = -20

See also

returns a sentinel indicating the end
(public member function) [edit]
iterator type of basic_vec and basic_mask
(exposition-only class template*)[edit]
(C++11)(C++14)
returns an iterator to the beginning of a container or array
(function template) [edit]