-
Hi, My current approach is this: #include <iostream>
#ifdef NANO
#include <nanobind/nanobind.h>
#else
#include <pybind11/pybind11.h>
#endif
// this is my wrapper
template <class R, class... Args>
struct wrap
{
using funct_type = R(*)(Args...);
funct_type func;
wrap(funct_type f): func(f) {};
R operator()(Args&&... args) const
{
//before code block
std::cout << "before calling\n";
R ret=func(std::forward<Args>(args)...);
//after code block
std::cout << "After calling\n";
return ret;
}
};
// This is an example of something I'd like to wrap
class bar {};
class foo : bar {
public:
enum xfoo { FOO=1, BAR=2, BAZ=3 };
static int add(int a, int b, enum xfoo c) { return a+b; }
static int add2(int a, int b) { return a+b; }
};
// this is the auto-generated interface
#ifdef NANO
NB_MODULE(example, m)
#else
PYBIND11_MODULE(example, m)
#endif
{
m.doc() = "example plugin"; // optional module docstring
// this works
std::cout << "Result" << wrap{foo::add}(1,2, foo::xfoo::FOO) << std::endl;
// so does this
m.def("add", foo::add, "A function that adds two numbers and no enum");
// this works with pybind11 but not nano
m.def("add2", wrap{foo::add2}, "A wrapped function that adds two numbers");
// this works with neither
m.def("add", wrap{foo::add}, "A wrapped function that adds two numbers and no enum");
} Ideas how to get this to work would be very much appreciated. Context: the wrapping |
Beta Was this translation helpful? Give feedback.
Answered by
wjakob
Jan 14, 2025
Replies: 1 comment
-
FYI, this code compiled for me (on Clang) after I added |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
smurfix
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI, this code compiled for me (on Clang) after I added
const
tooperator()