Skip to content

<xstd/misc/concepts/proxy_semi_reference.hpp>

xstd::proxy_semi_reference

template<class R, class V>
concept proxy_semi_reference = is_class_v<R> and (not same_as<R, V>) and convertible_to<R, V>;

A class that stands in for a value of type V, which the caller names, and converts to it.

Models

  • Every library's vector<bool>::reference and bitset<N>::reference models it for bool.
  • A class that converts to the named V models it, with or without a way back from its address.
  • V itself, a scalar, a class naming a value it does not convert to, and a language reference do not.

Example

#include <xstd/misc/concepts/proxy_semi_reference.hpp> // proxy_semi_reference
#include <bitset>                                      // bitset
#include <iostream>                                    // boolalpha, cout
#include <vector>                                      // vector

// What a generic function is handed for one bit: a bool, or a class that stands in for one.
template<class R>
auto print(R const& bit) -> void
{
        if constexpr (xstd::proxy_semi_reference<R, bool>) {
                std::cout << "a proxy for bool, reading " << static_cast<bool>(bit) << '\n';
        } else {
                std::cout << "a bool, reading " << bit << '\n';
        }
}

auto main() -> int
{
        auto bits       = std::vector<bool>{true, false};
        auto flags      = std::bitset<2>("01");
        auto const flag = true;

        std::cout << std::boolalpha;
        print(bits[0]);
        print(flags[0]);
        print(flag);

        // auto deduces the proxy, not the bool it stands for.
        auto const first = bits[1];
        print(first);
}

Output:

a proxy for bool, reading true
a proxy for bool, reading true
a bool, reading true
a proxy for bool, reading false

See also

Entity Relation
proxy_reference a semi-reference whose address leads back to it, and which writes through
proxy_semi_iterator an iterator whose reference models this concept