Skip to content

Solutions

Use these to check your work after you’ve tried the exercises. If something differs, treat it as a debugging opportunity.

Exercise 1

m.def("add", &add, "Add two integers and return the result.");

Exercise 2

In the binding file:

m.def("add", py::overload_cast<int,int>(&add), "Add ints");
m.def("add", py::overload_cast<double,double>(&add), "Add doubles");

(You need namespace py = pybind11;.)

Exercise 3

m.def("add", &add, py::arg("a"), py::arg("b"), "Add two integers.");