-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
I have two overloaded functions, one of which uses a template, how should I convert to the Python side? #3085
Comments
I feel that the previous example is too complicated, so I code a simplified example. #include <iostream>
#include <vector>
#include "pybind11/pybind11.h"
template <typename D>
D sum(const D &a, const D &b) {
return a + b;
}
std::vector<int> sum(const int &a, const int &b) {
std::vector<int> list;
list.push_back(a);
list.push_back(b);
return list;
}
PYBIND11_MODULE(mymath, m) {
m.def("sum", &sum<int>);
m.def("sum", &sum<double>);
m.def("sum", pybind11::overload_cast<const int &, const int &>(&sum));
} |
Hi @sun1638650145 , First thing please look at the other comment I (on other account) made about overloading functions: #3035 Don't forget you are always able to use lambdas to resolve issues with overloading. This is way simpler solution and clean as well (in my opinion). One more thing is to use Please try this out and let me know how it works for you! 😄 #include <iostream>
#include <vector>
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
template <typename D>
D sum(const D &a, const D &b)
{
return a + b;
}
std::vector<int> sum(const int &a, const int &b)
{
std::vector<int> list;
list.push_back(a);
list.push_back(b);
return list;
}
PYBIND11_MODULE(mymodule, m)
{
// m.def("sum", &sum<int>);
m.def("sum", &sum<double>);
m.def("sum", [](const int &a, const int &b)
{ return sum(a, b); });
} |
@jiwaszki First of all, thank you very much for your help. However, because of my reasons, the previous examples cannot explain my problem very well. Using Lambda can only solve the problem of static functions, and the member functions of the class seem to be infeasible. I code a new example again. Finally, thank you again. #include "pybind11/eigen.h"
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
class Foo {
public:
unsigned int seed = 1234;
template<typename T>
std::variant<int, std::vector<Eigen::MatrixXf>>
sum (const T &a, const T &b) {
if (typeid(a) == typeid(int)) {
return a + b;
}
if (typeid(a) == typeid(Eigen::MatrixXf)) {
std::vector<T> list;
list.push_back(this->seed * a);
list.push_back(this->seed * b);
return list;
}
}
};
PYBIND11_MODULE(mymath, m) {
pybind11::class_<Foo>(m, "Foo")
.def(pybind11::init())
.def("sum", pybind11::overload_cast<const int &, const int &>(&Foo::sum<int>))
.def("sum", pybind11::overload_cast<const Eigen::MatrixXf &, const Eigen::MatrixXf &>
(&Foo::sum<std::vector<Eigen::MatrixXf>>));
} |
Thanks @sun1638650145 , that shed some light on the problem. Once again I will advise to use lambdas in here. You also need to add all possible returning types to Looking forward to get your feedback on it. Code is here on gist: Note: If you want to invoke the methods like |
@jiwaszki Thanks for your help, I solved my problem. |
thanks for your help |
This is the class I defined.
This conversion is not successful, it seems that
pybind11::overload_cast
does not work.This is the error log.
The text was updated successfully, but these errors were encountered: