Calico
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups
cal::null_terminated_iterator< T > Class Template Reference

A ForwardIterator that traverses a null-terminated array. More...

#include <string.hpp>

Public Types

typedef std::ptrdiff_t difference_type
 Difference type.
 
typedef std::forward_iterator_tag iterator_category
 Iterator category.
 
typedef value_typepointer
 Pointer type.
 
typedef value_typereference
 Reference type.
 
typedef T value_type
 Value type.
 

Public Member Functions

 null_terminated_iterator (pointer ptr, bool end=false)
 Constructs a null-terminated iterator from a pointer. More...
 
bool operator!= (const null_terminated_iterator &other) const
 Compares two iterators for inequality.
 
reference operator* () const
 Dereferences the iterator.
 
null_terminated_iteratoroperator++ ()
 Increments the iterator and returns the incremented iterator.
 
null_terminated_iterator operator++ (int)
 Increments and returns the iterator prior to the increment.
 
pointer operator-> () const
 Member access of the element pointed to by the iterator.
 
bool operator== (const null_terminated_iterator &other) const
 Compares two iterators for equality.
 

Detailed Description

template<class T>
class cal::null_terminated_iterator< T >

A ForwardIterator that traverses a null-terminated array.

Template Parameters
TThe element type of the array. Must be DefaultConstructible, Destructible, and EqualityComparable.

A null-terminated array is an array of elements in which the past-the-end element is determined by a default-constructed value of the element type. Elements before the past-the-end element are forbidden to have the same value as the default-constructed value. A typical example would be a null-terminated array of chars (a plain C string).

This iterator allows forward iteration from the first value of the array to the last value of the array, just before the past-the-end element. Elements beyond the past-the-end element are not considered to be part of the null-terminated array.

The construction of this iterator does not require the position of the past-the-end element and thus it has O(1) complexity with respect to the length of the string.

The following actions will invoke undefined behavior:

  • Traversing beyond the range of the null-terminated array.
  • Dereferencing the past-the-end iterator.
  • Comparing equality or inequality with iterators constructed from different pointers.
  • Using the iterator in any way after the array has been invalidated.

Constructor & Destructor Documentation

template<class T >
cal::null_terminated_iterator< T >::null_terminated_iterator ( pointer  ptr,
bool  end = false 
)
inlineexplicit

Constructs a null-terminated iterator from a pointer.

Parameters
ptrA pointer to the first element.
endWhether a past-the-end iterator is to be constructed.