-
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
Template functions #199
Comments
You can only bind explicitly instantiated versions of your function, e.g. |
Ah, OK! Just for reference, this works:
|
Also just for reference, for classes it works like so
|
@PaulSt: what do you need the shared_ptr for in the class binding? |
If I don't use template functions, but still have overloaded functions, for example (for some declarations):
Does this mean pybind11 has no solution for this? Even if I want to map the first |
Ah, I need to use |
How would one bind a template function where the template argument is behind other arguments?
|
|
For anyone coming across this post confused on how to bind template member functions, I have a solution / questions that would probably be useful to share.
class Foo {
template<typename T>
void foo(T t) {
...
}
};
// naive bind that seems to sometimes work and sometimes not work
m.def("foo", &Foo::foo<double>);
// use this to bind (also works for static members), replacing double with whatever specialization
m.def("foo", &Foo::template foo<double>); Does this make any sense? Any thoughts? |
Unless |
@bstaletic That must be it, I've got helper functions that look like the following and that seems to be where you're saying the template<typename T>
auto bind_XX_type(py::class_<T,...>& c) {
return c.def("bar", &T::bar)
.def("foo", &T::template foo<double>);
}
bind_XX_type(py::class_<Foo>(m, ...)); |
@jlucier Yep, that must be it. That's probably a "dependent name", then, if I got my terminology correct. |
@PaulSt Can you please take a look in this example. My goal is to only bind a class template. I will not inherit that one in CPP side. Only a
And then I have followed your approch but as I have not so deep idea I am stumbling
Code is compiled successfully but while I have imported the module in |
Got the solution. The problem was linking
Idea of using class template header file taken from here |
@atifkarim, this issue was already closed. It's not the right place or moment to come asking more questions. There's also Gitter to ask questions, rather than adding questions to an old finished thread. (The previous things seemed to be short extra answers, rather than a whole new big question.) |
Is it possible to define template functions like
I always get error messages like
or, when using
py::vectorize
The text was updated successfully, but these errors were encountered: