What version (or hash if on master) of pybind11 are you using?
2.10.4
Problem description
I often find myself wrapping C++ templates using pybind11. In this case, I can write a template function that instantiates and registers a specific form of the template. Of course, if I do this for multiple instantiations, I end up with multiple python classes, and those should have distinct names. I would like to generate these names in the template function at runtime. The problem is that the class_ constructors only take const char * for the name, providing no way of managing the dynamic memory where the runtime-generated name resides (a naive use of std::string::c_str() obviously leads to dangling references).
Python's type system probably wasn't designed with C++ templates in mind, so it likely doesn't provide an direct way of resource management for the name of a type object. However, we can make a workaround using weak references, following the example given in the pybind11 documentation. I have been using the following:
Required prerequisites
What version (or hash if on master) of pybind11 are you using?
2.10.4
Problem description
I often find myself wrapping C++ templates using pybind11. In this case, I can write a template function that instantiates and registers a specific form of the template. Of course, if I do this for multiple instantiations, I end up with multiple python classes, and those should have distinct names. I would like to generate these names in the template function at runtime. The problem is that the
class_
constructors only takeconst char *
for the name, providing no way of managing the dynamic memory where the runtime-generated name resides (a naive use ofstd::string::c_str()
obviously leads to dangling references).Python's type system probably wasn't designed with C++ templates in mind, so it likely doesn't provide an direct way of resource management for the name of a type object. However, we can make a workaround using weak references, following the example given in the pybind11 documentation. I have been using the following:
I wonder if something like this could be reasonably included in a dedicated constructor?
Reproducible example code
No response
Is this a regression? Put the last known working version here if it is.
Not a regression