title: how to wrap class template consisting virtual function with pybind
body: <p>A <code>class template</code> consists of <code>pure virtual function</code> and inherited by another class. <em><strong>I am not getting the right path to bind it</strong></em>. Following <a href="https://gist.github.com/atifkarim/5ac63ac5ea918c9ab1ac09588aa01fe4" rel="nofollow noreferrer">Error Message</a> has appeared while trying to compile.<br />
  I have gone through the following link to find the solution <a href="https://stackoverflow.com/questions/52682145/error-type-value-mismatch-at-argument-1-in-template-parameter-list-for-templat">1</a>, <a href="https://stackoverflow.com/questions/47487888/pybind11-template-class-of-many-types">2</a>, <a href="https://github.com/pybind/pybind11/issues/116" rel="nofollow noreferrer">3</a>, <a href="https://github.com/pybind/pybind11/pull/117" rel="nofollow noreferrer">4</a> but don't understand what to do. I am pasted my code here.</p>
  <p><strong>folder structure</strong></p>
  <pre><code>proj
    -include/sample_code.h
    -lib/pybind11
    -scripts/cpp_py_cmake.py (also here pybind module stores)
    -src/
      -binding.cpp
    -CMakeLists.txt
  </code></pre>
  <p><code>sample_code.h</code></p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  /**
   * class template with virtual function */
  
  template&lt;typename T = int&gt;
  class Base_Abstract
  {
  protected:
      T base_Variable;
  
  public:
      Base_Abstract(T x):base_Variable{x}{
          std::cout&lt;&lt;&quot;Base_Abstract Class Constructor is called and Mem Var: &quot;&lt;&lt;base_Variable&lt;&lt;std::endl;
      }
      virtual void do_something_abstract(T val) = 0;
  
      virtual ~Base_Abstract() {}
  };
  
  template&lt;typename T = int&gt;
  class Derived_Abstract : public Base_Abstract&lt;T&gt;
  {
      public:
          Derived_Abstract(T a, T b):Base_Abstract&lt;T&gt;{b}, derived_Variable{a}{
              std::cout&lt;&lt;&quot;Derived_Abstract Class Constructor is called and a: &quot;&lt;&lt;a&lt;&lt;&quot; ,derived_var: &quot;&lt;&lt;derived_Variable&lt;&lt;&quot; , val for base: &quot;&lt;&lt;b&lt;&lt;std::endl;
          }
          void show_derived_value(){ 
              cout&lt;&lt;&quot;show_derived_value() of  Derived_Abstract class derived_Variable: &quot;&lt;&lt;derived_Variable&lt;&lt;endl;
          };
          void do_something_abstract(T g) override{
              cout&lt;&lt;&quot;virtual function is called from Derived_Abstract class when g: &quot;&lt;&lt;g&lt;&lt;endl;
          };
  
          virtual ~Derived_Abstract(){}
  
      private:
          T derived_Variable;
  
  };
  
  </code></pre>
  <p><strong>pybind related code</strong>
  <code>binding.cpp</code></p>
  <pre><code>#include &lt;pybind11/pybind11.h&gt;
  #include &quot;sample_code.h&quot;
  namespace py = pybind11;
  
  /**
   * class template which is abstract class and inherited
   */
  
  template &lt;typename T = int&gt;
  void declare_class_template_abstarct(py::module &amp;m, const std::string&amp; typestr1)
  {
      class PyVirtualClass : public Base_Abstract &lt;T&gt;{
          public:
              using Base_1 = Base_Abstract&lt;T&gt;;
  
              /* Trampoline (need one for each virtual function) */
              void do_something_abstract(int val) override {
                  PYBIND11_OVERRIDE_PURE(
                      void, /* Return type */
                      Base_1,      /* Parent class */
                      do_something_abstract,          /* Name of function in C++ (must match Python name) */
                      val      /* Argument(s) */
                      );
              }
  
      };
  
      py::class_&lt;Base_Abstract &lt;T&gt;, PyVirtualClass /* &lt;--- trampoline*/&gt;(m,&quot;Base_Abstract&quot;)
          .def(py::init&lt;T&gt;())
          .def(&quot;do_something_abstract&quot;, &amp;Base_Abstract &lt;T&gt;::do_something_abstract);
  
      py::class_&lt;Derived_Abstract&lt;T&gt;, Base_Abstract&lt;T&gt;&gt;(m, &quot;Derived_Abstract&quot;)
          .def(py::init&lt;T,T&gt;())
          .def(&quot;show_derived_value&quot;, &amp;Derived_Abstract &lt;T&gt;::show_derived_value)
          .def(&quot;do_something_abstract&quot;, &amp;Derived_Abstract &lt;T&gt;::do_something_abstract);
  };
  
  PYBIND11_MODULE(somecode, m) 
  {
      declare_class_template_abstarct&lt;int&gt;(m, &quot;int&quot;);
  }
  
  /*
  &lt;%
  setup_pybind11(cfg)
  %&gt;
  */
  </code></pre>
  <p><strong>CMakeLists.txt</strong> file used to compile the code</p>
  <pre><code>cmake_minimum_required(VERSION 3.10)
  project(somecode)
  
  set(LIB_GEN_PATH ${PROJECT_SOURCE_DIR}/scripts CACHE STRING &quot;Where generated library will be placed&quot;)
  
  add_subdirectory(lib/pybind11)
  
  include_directories(${PROJECT_SOURCE_DIR}/include)
  
  pybind11_add_module(${PROJECT_NAME} 
                      ${PROJECT_SOURCE_DIR}/src/binding.cpp)
  
  set_target_properties(${PROJECT_NAME}
      PROPERTIES
      LIBRARY_OUTPUT_DIRECTORY ${LIB_GEN_PATH}
  )
  
  </code></pre>
  <p><code>cpp_py_cmake.py</code></p>
  <pre><code>import somecode
  
  class_temp_abstract_class_obj = somecode.Derived_Abstract(42, 59)
  </code></pre>
  <p>Any suggestion is highly appreciable.</p>