Struct Struct::iterator#

Nested Relationships#

This struct is a nested type of Struct Struct.

Struct Documentation#

struct iterator

The Struct::iterator class implements the typical C++ iterator protocol and conveniently provides access to the field names and values of a Struct. It is created by calling either Struct::begin() or Struct::end().

Make sure to compare the iterator to the iterator returned by Struct::end() before de-referencing it. The value returned when de-referencing is a std::pair that holds a std::string_view of the field name as well as a const reference of the value. Both references become invalid when the iterator or the Struct is changed, so make sure to make copies if you want to retain the name or value.

Note that the order in which the iterator exposes the fields is not defined.

If you’re using C++ 17, you can use the convenience destructuring syntax to extract the name and value in one go:

Struct stru = ...;
auto it = stru.begin();
...
++it; // advance iterator to the next field
...
// Check iterator before dereferencing it
if (it != stru.end()) {
    // Extract a view of the name and a const reference to the value in one go.
    auto [field_name, field_value] = *it;
}

Public Types

using value_type = std::pair<std::string_view, const Value&>

A typedef for std::pair<std::string_view, const Value &> that’s returned when dereferencing the iterator.

Public Functions

inline ~iterator()

Destroys this field iterator.

iterator(const iterator&) = delete
iterator &operator=(const iterator&) = delete
iterator(iterator &&other) = default

Move-constructs a new iterator from other.

iterator &operator=(iterator &&other) = default

Move-assigns the iterator other to this and returns a reference to this.

inline iterator &operator++()

The prefix ++ operator advances the iterator to the next entry and returns a reference to this.

inline value_type operator*() const

Dereferences the iterator to return a pair of the key and value.

Friends

inline friend bool operator==(const iterator &a, const iterator &b)

Returns true if a is pointing to the same entry as b; false otherwise.

inline friend bool operator!=(const iterator &a, const iterator &b)

Returns false if a is pointing to the same entry as b; true otherwise.