constant_iterator#

template<class _Tp, class _Index = ::cuda::std::ptrdiff_t>
class constant_iterator#

The constant_iterator class represents an iterator in an infinite sequence of repeated values.

This iterator is useful for creating a range filled with the same value without explicitly storing it in memory. Using constant_iterator saves both memory capacity and bandwidth.

The following code snippet demonstrates how to create a constant_iterator whose value_type is int and whose value is 10.

#include <cuda/iterator>

cuda::constant_iterator iter(10);

*iter;    // returns 10
iter[0];  // returns 10
iter[1];  // returns 10
iter[13]; // returns 10

// and so on...
Template Parameters:

Public Types

using iterator_concept = ::cuda::std::random_access_iterator_tag#
using iterator_category = ::cuda::std::random_access_iterator_tag#
using value_type = _Tp#
using difference_type = ::cuda::std::ptrdiff_t#

Public Functions

template<class _Tp2 = _Tp, ::cuda::std::enable_if_t<::cuda::std::default_initializable<_Tp2>, int> = 0>
inline constexpr constant_iterator(
) noexcept(::cuda::std::is_nothrow_default_constructible_v<_Tp2>)#
inline constexpr constant_iterator(
_Tp __value,
) noexcept(::cuda::std::is_nothrow_move_constructible_v<_Tp>)#

Creates a constant_iterator from a value.

The index is set to zero

Parameters:

__value – The value to store in the constant_iterator

template<typename _Index2, ::cuda::std::enable_if_t<::cuda::std::__integer_like<_Index2>, int> = 0>
inline explicit constexpr constant_iterator(
_Tp __value,
_Index2 __index,
) noexcept(::cuda::std::is_nothrow_move_constructible_v<_Tp>)#

Creates constant_iterator from a value and an index.

Parameters:
inline constexpr difference_type index() const noexcept#

Returns a the current index.

inline constexpr const _Tp &operator*() const noexcept#

Returns a const reference to the stored value.

inline constexpr const _Tp &operator[](
difference_type,
) const noexcept#

Returns a const reference to the stored value.

inline constexpr constant_iterator &operator++() noexcept#

Increments the stored index.

inline constexpr constant_iterator operator++(
int,
) noexcept(::cuda::std::is_nothrow_copy_constructible_v<_Tp>)#

Increments the stored index.

inline constexpr constant_iterator &operator--() noexcept#

Decrements the stored index.

inline constexpr constant_iterator operator--(
int,
) noexcept(::cuda::std::is_nothrow_copy_constructible_v<_Tp>)#

Decrements the stored index.

inline constexpr constant_iterator &operator+=(
difference_type __n,
) noexcept#

Advances a constant_iterator by a given number of elements.

Parameters:

__n – The amount of elements to advance

inline constexpr constant_iterator &operator-=(
difference_type __n,
) noexcept#

Decrements a constant_iterator by a given number of elements.

Parameters:

__n – The amount of elements to decrement

Friends

inline friend constexpr constant_iterator operator+(
constant_iterator __iter,
difference_type __n,
) noexcept#

Creates a copy of a constant_iterator advanced by a given number of elements.

Parameters:
  • __iter – The constant_iterator to advance

  • __n – The amount of elements to advance

inline friend constexpr constant_iterator operator+(
difference_type __n,
constant_iterator __iter,
) noexcept#

Creates a copy of a constant_iterator advanced by a given number of elements.

Parameters:
  • __n – The amount of elements to advance

  • __iter – The constant_iterator to advance

inline friend constexpr constant_iterator operator-(
constant_iterator __iter,
difference_type __n,
) noexcept#

Creates a copy of a constant_iterator decremented by a given number of elements.

Parameters:
  • __n – The amount of elements to decrement

  • __iter – The constant_iterator to decrement

inline friend constexpr difference_type operator-(
const constant_iterator &__lhs,
const constant_iterator &__rhs,
) noexcept#

Returns the distance between two constant_iterator.

inline friend constexpr bool operator==(
const constant_iterator &__lhs,
const constant_iterator &__rhs,
) noexcept#

Compares two constant_iterator for equality by comparing the index in the sequence.

inline friend constexpr bool operator<(
const constant_iterator &__lhs,
const constant_iterator &__rhs,
) noexcept#

Compares two constant_iterator for less than by comparing the index in the sequence.

inline friend constexpr bool operator<=(
const constant_iterator &__lhs,
const constant_iterator &__rhs,
) noexcept#

Compares two constant_iterator for less equal by comparing the index in the sequence.

inline friend constexpr bool operator>(
const constant_iterator &__lhs,
const constant_iterator &__rhs,
) noexcept#

Compares two constant_iterator for greater than by comparing the index in the sequence.

inline friend constexpr bool operator>=(
const constant_iterator &__lhs,
const constant_iterator &__rhs,
) noexcept#

Compares two constant_iterator for greater equal by comparing the index in the sequence.

template<class _Tp>
inline constexpr auto make_constant_iterator(
_Tp __value,
::cuda::std::ptrdiff_t __index = 0,
)#

Creates a constant_iterator from a value and an index.

Parameters:
  • __value – The value to be stored

  • __index – The optional index representing the position in a sequence. Defaults to 0.