Skip to content

Function template reflect (proxy_indirect_accessor<F>)

Since: 4.1.0

template <class R>
const R& reflect(const proxy_indirect_accessor<F>& p) noexcept;

Acquires reflection information of the contained type of the associated proxy, through an indirect reflection.

Let P be the contained type of the proxy object associated to p. Returns a const reference of R direct-non-list-initialized with std::in_place_type<typename std::pointer_traits<P>::element_type>.

There shall be a reflection type Refl defined in typename F::reflection_types where

  • Refl::is_direct is false, and
  • typename Refl::reflector_type is R.

The reference obtained from reflect() may be invalidated if the associated proxy is subsequently modified.

This function is not visible to ordinary unqualified or qualified lookup. It can only be found by argument-dependent lookup when proxy_indirect_accessor<F> is an associated class of the arguments.

A proxy_indirect_accessor<F> is obtained by dereferencing a proxy<F> (i.e., *p). To acquire a direct reflection (deduced from the pointer type), use reflect on the proxy<F> itself.

Notes

reflect was introduced in 4.1.0 as a replacement for the deprecated proxy_reflect. proxy_reflect is a namespace-scope function, while reflect is a non-member function of proxy_indirect_accessor found only via argument-dependent lookup.

This function is useful when only metadata deduced from a type is needed. While invoke can also retrieve type metadata, reflect can generate more efficient code in this context.

Example

#include <iostream>

#include <proxy/proxy.h>

class LayoutReflector {
public:
  template <class T>
  constexpr explicit LayoutReflector(std::in_place_type_t<T>)
      : Size(sizeof(T)), Align(alignof(T)) {}

  template <class P, class R>
  struct accessor {
    friend std::size_t SizeOf(const P& self) noexcept {
      const LayoutReflector& refl = reflect<R>(self);
      return refl.Size;
    }
  };

  std::size_t Size, Align;
};

struct LayoutAware : pro::facade_builder                        //
                     ::add_indirect_reflection<LayoutReflector> //
                     ::build {};

int main() {
  int a = 123;
  pro::proxy<LayoutAware> p = &a;
  std::cout << SizeOf(*p) << "\n"; // Prints sizeof(int), the pointed-to type
}

See Also