#include <iostream>
#include <memory>

template<typename T>
struct A {
  T value_;

  A(T value) : value_(value) {}

  void sayHello() const {
    std::cout << "Hello from A! " << value_ << '\n';
  }
};
template<typename T>
struct B {
  T value_;

  B(T value) : value_(value) {}

  void sayHello() const {
    std::cout << "Hello from B! " << value_ << '\n';
  }
};

class Wrapper {
private:
  class Concept {
  public:
    virtual ~Concept() = default;

    virtual void sayHello() const = 0;
  };

  template<typename T>
  class Model final
      : public Concept {
  private:
    T data_;

  public:
    Model(T data) : data_(data) {}

    virtual void sayHello() const override {
      data_.sayHello();
    }

  private:
    template<typename U>
    friend inline void doSomething(const Concept &lhs, const B<U> &rhs) {
      T x = static_cast<const Model<T> &>(lhs).data_;

      x.sayHello();
      rhs.sayHello();

      auto y = x.value_ + rhs.value_;

      std::cout << y << '\n';
    }
  };

  template<typename U>
  friend inline void doSomething(const Concept &lhs, const B<U> &rhs);

  std::shared_ptr<const Concept> ptr_;

public:
  template<typename T>
  explicit inline Wrapper(T a)
      : ptr_(std::make_shared<Model<A<T>>>(std::move(a))) {}

  template<typename U>
  friend inline void someFriend(Wrapper &lhs, B<U> &rhs) {
    doSomething(*lhs.ptr_, rhs);
  }
};

int main() {
  Wrapper a(1);
  B<int> b{2};

  someFriend(a, b);
  
  return 0;
}