Skip to content

Alias template as_weak

Header: proxy.h
Module: proxy
Namespace: pro::skills
Since: 4.0.0

template <class FB>
using as_weak = /* see below */;

The alias template as_weak modifies a specialization of basic_facade_builder to allow implicit conversion from proxy<F> to weak_proxy<F>, where F is a built facade type.

Let p be a value of type proxy<F>, ptr be the contained value of p (if any), Ptr be the type of ptr, the conversion from type const proxy<F>& to type weak_proxy<F> is equivalent to return typename Ptr::weak_type{p} if p contains a value, or otherwise equivalent to return nullptr.

Example

#include <iostream>

#include <proxy/proxy.h>

struct RttiAware : pro::facade_builder
    ::support<pro::skills::rtti>
    ::support<pro::skills::as_weak>
    ::build {};

int main() {
  pro::proxy<RttiAware> p1 = pro::make_proxy_shared<RttiAware>(123);
  pro::weak_proxy<RttiAware> p2 = p1;
  proxy_cast<int&>(*p1) = 456;  // Modifies the contained object of p1

  pro::proxy<RttiAware> p3 = p2.lock();
  p1.reset();
  std::cout << proxy_cast<int>(*p3) << "\n";  // Prints "456"

  p3.reset();
  pro::proxy<RttiAware> p4 = p2.lock();
  std::cout << std::boolalpha << p4.has_value() << "\n";  // Prints "false"
}

See Also