title: Is it possible in pybind11 to use py::cast to access an abstract base class?
body: <p>I have include a minimal working example below - it can be compiled using the typical pybind11 instructions (I use cmake).</p>
  
  <p>I have an abstract base class, <code>Abstract</code>, which is pure virtual. I can easily wrap this in pybind11 using a "trampoline class" (this is well documented by pybind11).</p>
  
  <p>Further, I have a concrete implementation of <code>Abstract</code>, <code>ToBeWrapped</code>, that is also wrapped using pybind11.</p>
  
  <p>My issue is that I have some client code which accepts an arbitrary <code>PyObject*</code> (or, in the case of this example, pybind11's wrapper <code>py::object</code>) and expects to cast this to <code>Abstract*</code>.</p>
  
  <p>However, as illustrated in my example, I am unable to cast the <code>py::object</code> to <code>Abstract*</code>.</p>
  
  <p>I have no problem casting to <code>ToBeWrapped*</code> and then storing that as an <code>Abstract*', however this would require my client code to know ahead of time what kind of</code>Abstract*` the python interpreter is sending, which defeats the purpose of the abstract base class.</p>
  
  <p><strong>TL;DR</strong></p>
  
  <p>Is it possible to modify this code such that the client <code>accessMethod</code> is able to arbitrarily handle an <code>Abstract*</code> passed from the python interpreter?</p>
  
  <pre><code>#include &lt;pybind11/pybind11.h&gt;
  #include &lt;iostream&gt;
  
  namespace py = pybind11;
  
  // abstract base class - cannot be instantiated on its own
  class Abstract
  {
      public:
          virtual ~Abstract() = 0;
          virtual std::string print() const = 0;
  };
  
  Abstract::~Abstract(){}
  
  // concrete implementation of Abstract
  class ToBeWrapped : public Abstract
  {
      public:
          ToBeWrapped(const std::string&amp; msg = "heh?")
              : myMessage(msg){};
          std::string print() const override
          {
              return myMessage;
          }
  
      private:
          const std::string myMessage;
  };
  
  // We need a trampoline class in order to wrap this with pybind11
  class AbstractPy : public Abstract
  {
      public:
          using Abstract::Abstract;
  
          std::string print() const override
          {
              PYBIND11_OVERLOAD_PURE(
                  std::string, // return type
                  Abstract, // parent class
                  print, // name of the function
                  // arguments (if any)
              );
          }
  };
  
  // I have client code that accepts a raw PyObject* - this client code base implements its
  // own python interpreter, and calls this "accessMethod" expecting to convert the python
  // object to its c++ type.
  //
  // Rather than mocking up the raw PyObject* method (which would be trivial) I elected to
  // keep this minimal example 100% pybind11
  void accessMethod(py::object obj)
  {
      // runtime error: py::cast_error
      //Abstract* casted = obj.cast&lt;Abstract*&gt;();
  
      // this works
      Abstract* casted = obj.cast&lt;ToBeWrapped*&gt;();
  }
  
  PYBIND11_MODULE(PyMod, m)
  {
      m.doc() = R"pbdoc(
      This is a python module
      )pbdoc";
  
      py::class_&lt;Abstract, AbstractPy&gt;(m, "Abstract")
          .def("print", &amp;Abstract::print)
      ;
  
      py::class_&lt;ToBeWrapped&gt;(m, "WrappedClass")
          .def(py::init&lt;const std::string&amp;&gt;())
      ;
      m.def("access", &amp;accessMethod, "This method will attempt to access the wrapped type");
  }
  </code></pre>