Struct index_t

Synopsis

#include <include/type_safe/index.hpp>

struct index_t : strong_typedef<index_t, size_t>,
                 strong_typedef_op::equality_comparison<index_t>,
                 strong_typedef_op::relational_comparison<index_t>,
                 strong_typedef_op::increment<index_t>,
                 strong_typedef_op::decrement<index_t>,
                 strong_typedef_op::unary_plus<index_t>

Description

A type modelling an index into an array.

It is a [ts::strong_typedef]() for [ts::size_t](). It is comparable and you can increment and decrement it, as well as adding/subtracting a [ts::distance_t](). \notes It has a similar interface to a RandomAccessIterator, but without the dereference functions. \module types

Mentioned in

Inheritance

Ancestors: increment, decrement, equality_comparison, unary_plus, relational_comparison, strong_typedef

Methods

index_t overload\effects Initializes it to 0.
index_t overload\effects Initializes it from a valid unsigned integer type
index_t overload\group int_ctor
operator+=\effects Advances the index by the distance specified in rhs
operator-=\effects Advances the index backwards by the distance specified in rhs

Source

Lines 61-108 in include/type_safe/index.hpp.

struct index_t : strong_typedef<index_t, size_t>,
                 strong_typedef_op::equality_comparison<index_t>,
                 strong_typedef_op::relational_comparison<index_t>,
                 strong_typedef_op::increment<index_t>,
                 strong_typedef_op::decrement<index_t>,
                 strong_typedef_op::unary_plus<index_t>
{
    /// \effects Initializes it to `0`.
    constexpr index_t() noexcept : strong_typedef(0u) {}

    /// \effects Initializes it from a valid `unsigned` integer type.
    /// \notes This constructor does not participate in overload resolution,
    /// if `T` is not safely convertible to [ts::size_t]().
    /// \group int_ctor
    /// \param 1
    /// \exclude
    template <typename T, typename = typename std::enable_if<
                              detail::is_safe_integer_conversion<T, std::size_t>::value>::type>
    constexpr index_t(T i) noexcept : strong_typedef(i)
    {}

    /// \group int_ctor
    /// \param 1
    /// \exclude
    template <typename T, class Policy,
              typename = typename std::enable_if<
                  detail::is_safe_integer_conversion<T, std::size_t>::value>::type>
    constexpr index_t(integer<T, Policy> i) noexcept : strong_typedef(static_cast<T>(i))
    {}

    /// \effects Advances the index by the distance specified in `rhs`.
    /// If `rhs` is a negative distance, it advances backwards.
    /// \requires The new index must be greater or equal to `0`.
    index_t& operator+=(const difference_t& rhs) noexcept
    {
        get(*this) = make_unsigned(make_signed(get(*this)) + get(rhs));
        return *this;
    }

    /// \effects Advances the index backwards by the distance specified in `rhs`.
    /// If `rhs` is a negative distance, it advances forwards.
    /// \requires The new index must be greater or equal to `0`.
    index_t& operator-=(const difference_t& rhs) noexcept
    {
        get(*this) = make_unsigned(make_signed(get(*this)) - get(rhs));
        return *this;
    }
};





Add Discussion as Guest

Log in