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

Bind operator overloads #64

Open
kwabenantim opened this issue Nov 11, 2024 · 0 comments
Open

Bind operator overloads #64

kwabenantim opened this issue Nov 11, 2024 · 0 comments
Assignees

Comments

@kwabenantim
Copy link
Member

kwabenantim commented Nov 11, 2024

Summary

Automatically generate bindings for overloaded operators. For example:

class Foo
{
public:
  Foo();
  ~Foo();
  double& operator[](unsigned index);
};

An example of generated bindings for the overloaded operator[]:

void register_Foo_class(py::module &m)
{
  py::class_<Foo>(m, "Foo")
    // ...
    .def("__setitem__", &Foo::operator[], "")
    .def("__getitem__", &Foo::operator[], "")
  ;
}

This can currently be added in using custom templates:

class Foo(cppwg.templates.custom.Custom):

    def get_class_cpp_def_code(self, class_name):
        code = """\
        .def("__getitem__", &{class_name}::operator[])
        .def("__setitem__", &{class_name}::operator[])
        """.format(class_name=class_name)

        return code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant