Skip to content

Instantly share code, notes, and snippets.

@atifkarim
Last active February 22, 2021 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifkarim/7c8854fe431e9ec8bbc3dd9868fe9467 to your computer and use it in GitHub Desktop.
Save atifkarim/7c8854fe431e9ec8bbc3dd9868fe9467 to your computer and use it in GitHub Desktop.
multiple_typename_pybind
#include <pybind11/pybind11.h>
#include <pybind11/complex.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
#include <pybind11/numpy.h>
#include "class_template_abstract_class_only.h"
namespace py = pybind11;
template<typename T1 = int, typename T2 = int>
void unique_abstarct(py::module &m, const std::string& typestr1, const std::string& typestr2)
{
class PyVirtualClass_Abstract : public Base_Abstract_Only<T1, T2>{
public:
using Base_Abstract_Only<T1, T2>::Base_Abstract_Only;
/* Trampoline (need one for each virtual function) */
void virtual_abstract(T2 val) override {
PYBIND11_OVERRIDE_PURE(
void, /* Return type */
Base_Abstract_Only<T1, T2>, /* Parent class */
virtual_abstract, /* Name of function in C++ (must match Python name) */
val /* Argument(s) */
);
}
};
py::class_<Base_Abstract_Only <T1, T2>, PyVirtualClass_Abstract /* <--- trampoline*/>(m,"Base_Abstract_Only")
.def(py::init<T1>())
.def("virtual_abstract", &Base_Abstract_Only <T1, T2>::virtual_abstract);
};
PYBIND11_MODULE(somecode, m)
{
unique_abstarct<int, int>(m, "int", "int");
}
#ifndef _CLASS_TEMPLATE_ABSTRACT_CLASS_ONLY_
#define _CLASS_TEMPLATE_ABSTRACT_CLASS_ONLY_
#include <iostream>
using namespace std;
/**
* class template with virtual function */
template<typename T1 = int, typename T2 = int>
class Base_Abstract_Only
{
protected:
T1 base_Variable_Unique;
public:
Base_Abstract_Only(T1 x):base_Variable_Unique{x}{
std::cout<<"Base_Abstract Class Constructor is called and base_Variable_Unique: "<<base_Variable_Unique<<std::endl;
}
virtual void virtual_abstract(T2 val) = 0;
virtual ~Base_Abstract_Only() {}
};
#endif
cmake_minimum_required(VERSION 3.10)
project(somecode)
set(LIB_GEN_PATH ${PROJECT_SOURCE_DIR}/scripts CACHE STRING "Where generated library will be placed")
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}
)
[ 11%] Building CXX object CMakeFiles/somecode.dir/src/binding.cpp.o
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/src/binding.cpp:121:17: error: expected ')'
PYBIND11_OVERRIDE_PURE(
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2346:5: note: expanded from macro 'PYBIND11_OVERRIDE_PURE'
PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2310:9: note: expanded from macro 'PYBIND11_OVERRIDE_PURE_NAME'
PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2270:89: note: expanded from macro 'PYBIND11_OVERRIDE_IMPL'
pybind11::function override = pybind11::get_override(static_cast<const cname *>(this), name); \
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/src/binding.cpp:121:17: note: to match this '('
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2346:5: note: expanded from macro 'PYBIND11_OVERRIDE_PURE'
PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2310:9: note: expanded from macro 'PYBIND11_OVERRIDE_PURE_NAME'
PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2270:88: note: expanded from macro 'PYBIND11_OVERRIDE_IMPL'
pybind11::function override = pybind11::get_override(static_cast<const cname *>(this), name); \
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/src/binding.cpp:121:17: error: expected '>'
PYBIND11_OVERRIDE_PURE(
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2346:5: note: expanded from macro 'PYBIND11_OVERRIDE_PURE'
PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2310:9: note: expanded from macro 'PYBIND11_OVERRIDE_PURE_NAME'
PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2270:94: note: expanded from macro 'PYBIND11_OVERRIDE_IMPL'
pybind11::function override = pybind11::get_override(static_cast<const cname *>(this), name); \
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/src/binding.cpp:121:17: note: to match this '<'
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2346:5: note: expanded from macro 'PYBIND11_OVERRIDE_PURE'
PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2310:9: note: expanded from macro 'PYBIND11_OVERRIDE_PURE_NAME'
PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2270:73: note: expanded from macro 'PYBIND11_OVERRIDE_IMPL'
pybind11::function override = pybind11::get_override(static_cast<const cname *>(this), name); \
^
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/src/binding.cpp:124:21: error: reference to non-static member function must be called
virtual_abstract, /* Name of function in C++ (must match Python name) */
^~~~~~~~~~~~~~~~
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2346:89: note: expanded from macro 'PYBIND11_OVERRIDE_PURE'
PYBIND11_OVERRIDE_PURE_NAME(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), #fn, fn, __VA_ARGS__)
^~~~~~~~~~~
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2310:85: note: expanded from macro 'PYBIND11_OVERRIDE_PURE_NAME'
PYBIND11_OVERRIDE_IMPL(PYBIND11_TYPE(ret_type), PYBIND11_TYPE(cname), name, __VA_ARGS__); \
^~~~~~~~~~~
/net/users/Linux_Home/karim/my_test_code/pybind_cpp_cmake/lib/pybind11/include/pybind11/pybind11.h:2272:31: note: expanded from macro 'PYBIND11_OVERRIDE_IMPL'
auto o = override(__VA_ARGS__); \
^~~~~~~~~~~
3 errors generated.
CMakeFiles/somecode.dir/build.make:230: recipe for target 'CMakeFiles/somecode.dir/src/binding.cpp.o' failed
make[2]: *** [CMakeFiles/somecode.dir/src/binding.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/somecode.dir/all' failed
make[1]: *** [CMakeFiles/somecode.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment