Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting wrapped functions doesn't work #466

Open
smurfix opened this issue Jan 10, 2025 · 0 comments
Open

Exporting wrapped functions doesn't work #466

smurfix opened this issue Jan 10, 2025 · 0 comments

Comments

@smurfix
Copy link

smurfix commented Jan 10, 2025

I find myself in need of exporting a whole lot of functions to Python which require a wrapper (the API needs locking and whatnot).

The following code probably should work (pybind11 can handle it, for the most part), but doesn't.

#include <iostream>
#include <boost/python.hpp>

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)
    {
          std::cout << "before calling\n";
          R ret=func(std::forward<Args>(args)...);
          std::cout << "After calling\n";
          return ret;
    }
};

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; }
};

BOOST_PYTHON_MODULE(example)
{
    using namespace boost::python;

    // this works
    std::cout << "Result" << wrap{foo::add}(1,2, foo::xfoo::FOO) << std::endl;

    // so does this
    def("add", foo::add, "A function that adds two numbers and no enum");

    // this works with pybind11 but not boost
    def("add2", wrap{foo::add2}, "A function that adds two numbers");

    // this works with neither
    def("add", wrap{foo::add}, "A function that adds two numbers and no enum");
}
@smurfix smurfix changed the title Expoorting wrapped functions doesn't work Exporting wrapped functions doesn't work Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant