Skip to content

<xstd/misc/concepts/proxy_semi_iterator.hpp>

xstd::proxy_semi_iterator

template<class I>
concept proxy_semi_iterator =
  indirectly_readable<I> and proxy_semi_reference<iter_reference_t<I>, iter_value_t<I>>;

An iterator whose reference is a proxy for its value type.

Models

  • An iterator whose reference is a class prvalue standing in for its value type models it: vector<bool>'s iterator, zip_view's.
  • A pointer, an iterator yielding a language reference, and one yielding its value type by value do not.

Example

#include <xstd/misc/concepts/proxy_semi_iterator.hpp> // proxy_semi_iterator
#include <iostream>                                   // boolalpha, cout
#include <iterator>                                   // iter_value_t, next
#include <vector>                                     // vector

// Copies out the element an iterator points to, as its value type rather than as the proxy a semi-iterator yields.
template<class I>
[[nodiscard]] auto value_at(I it)
        -> std::iter_value_t<I>
{
        if constexpr (xstd::proxy_semi_iterator<I>) {
                std::cout << "through a proxy: ";
        } else {
                std::cout << "through a language reference: ";
        }
        return *it;
}

auto main() -> int
{
        auto bits        = std::vector<bool>{false, true};
        auto const words = std::vector<int>{1, 2, 3};

        std::cout << std::boolalpha;
        std::cout << value_at(std::next(bits.begin())) << '\n';
        std::cout << value_at(words.begin()) << '\n';
        std::cout << value_at(words.data()) << '\n';

        std::cout << "proxy_semi_iterator<vector<bool>::iterator> is " << xstd::proxy_semi_iterator<std::vector<bool>::iterator> << '\n';
        std::cout << "proxy_semi_iterator<vector<int>::iterator> is " << xstd::proxy_semi_iterator<std::vector<int>::iterator> << '\n';
        std::cout << "proxy_semi_iterator<int*> is " << xstd::proxy_semi_iterator<int*> << '\n';
}

Output:

through a proxy: true
through a language reference: 1
through a language reference: 1
proxy_semi_iterator<vector<bool>::iterator> is true
proxy_semi_iterator<vector<int>::iterator> is false
proxy_semi_iterator<int*> is false

See also

Entity Relation
proxy_semi_reference what its reference models
proxy_iterator the stronger concept, whose reference is a proxy reference