shared_ptr for stl types. Getting "Holder classes are only supported for custom types" #5320
Unanswered
DmitriGoloubentsev
asked this question in
Q&A
Replies: 1 comment
-
Sorry, file got corrupted. Here is the proper version. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
I'm trying to support C++ interfaces there shared_ptr<> is used to hold stl types such as std::vector<> etc. I'm getting "Holder classes are only supported for custom types" and saw suggestions to make enclosing type as OPAQUE. But this means more trouble on python side and custom classes.
I don't mind if data is copied and happy to use default stl binder. How do I achieve this for processVector and processVectorofA?
processVectorofA2 - works fine now, and I don't understand why processVector and processVectorofA fail.
Kind regards,
Dmitri.
`
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
// #include <pybind11/stl_bind.h>
#include
#include
#include
namespace py = pybind11;
void processVector(const std::shared_ptr<std::vector>& vec) {
// Example processing
for (double val : *vec) {
std::cout << val << std::endl;
}
}
class A {
public:
A() = default;
A(int i) : i(i) {}
int i;
};
void processVectorofA(const std::shared_ptr<std::vector>& vec) {
// Example processing
for (const A& val : *vec) {
std::cout << val.i << std::endl;
}
}
void processVectorofA2(const std::vector& vec) {
// Example processing
for (const A& val : vec) {
std::cout << val.i << std::endl;
}
}
PYBIND11_MODULE(example, m) {
py::class_(m, "A")
.def(py::init())
.def_readwrite("i", &A::i);
}
`
Compilation command:
clang++ -I./pybind11/include/ -c test_pybind.cpp -isystem /usr/include/python3.11
Error message:
`
In file included from test_pybind.cpp:1:
In file included from ./pybind11/include/pybind11/pybind11.h:13:
In file included from ./pybind11/include/pybind11/detail/class.h:12:
In file included from ./pybind11/include/pybind11/attr.h:14:
./pybind11/include/pybind11/cast.h:746:5: error: static_assert failed due to requirement 'std::is_base_of<pybind11::detail::type_caster_base<std::vector<double, std::allocator>>, pybind11::detail::type_caster<std::vector<double, std::allocator>, void>>::value' "Holder classes are only supported for custom types"
static_assert(std::is_base_of<base, type_caster>::value,
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./pybind11/include/pybind11/cast.h:820:48: note: in instantiation of template class 'pybind11::detail::copyable_holder_caster<std::vector, std::shared_ptr<std::vector>>' requested here
class type_caster<std::shared_ptr> : public copyable_holder_caster<T, std::shared_ptr> {};
^
./pybind11/include/pybind11/cast.h:1404:57: note: in instantiation of template class 'pybind11::detail::type_caster<std::shared_ptr<std::vector>>' requested here
static constexpr auto arg_names = concat(type_descr(make_caster::name)...);
^
./pybind11/include/pybind11/pybind11.h:219:34: note: in instantiation of template class 'pybind11::detail::argument_loader<const std::shared_ptr<std::vector> &>' requested here
sizeof...(Args), cast_in::args_pos >= 0, cast_in::has_kwargs),
^
./pybind11/include/pybind11/pybind11.h:92:9: note: in instantiation of function template specialization 'pybind11::cpp_function::initialize<void (&)(const std::shared_ptr<std::vector> &), void, const std::shared_ptr<std::vector> &, pybind11::name, pybind11::scope, pybind11::sibling, char[46], pybind11::arg>' requested here
initialize(f, f, extra...);
^
./pybind11/include/pybind11/pybind11.h:1163:22: note: in instantiation of function template specialization 'pybind11::cpp_function::cpp_function<void, const std::shared_ptr<std::vector> &, pybind11::name, pybind11::scope, pybind11::sibling, char[46], pybind11::arg>' requested here
cpp_function func(std::forward(f),
^
test_pybind.cpp:42:7: note: in instantiation of function template specialization 'pybind11::module_::def<void ()(const std::shared_ptr<std::vector> &), char[46], pybind11::arg>' requested here
m.def("process_vector", &processVector, "A function that processes a vector of doubles",
^
In file included from test_pybind.cpp:1:
./pybind11/include/pybind11/pybind11.h:219:41: error: incomplete definition of type 'pybind11::detail::argument_loader<const std::shared_ptr<std::vector> &>'
sizeof...(Args), cast_in::args_pos >= 0, cast_in::has_kwargs),
~~~~~~~^~
./pybind11/include/pybind11/pybind11.h:92:9: note: in instantiation of function template specialization 'pybind11::cpp_function::initialize<void (&)(const std::shared_ptr<std::vector> &), void, const std::shared_ptr<std::vector> &, pybind11::name, pybind11::scope, pybind11::sibling, char[46], pybind11::arg>' requested here
initialize(f, f, extra...);
^
./pybind11/include/pybind11/pybind11.h:1163:22: note: in instantiation of function template specialization 'pybind11::cpp_function::cpp_function<void, const std::shared_ptr<std::vector> &, pybind11::name, pybind11::scope, pybind11::sibling, char[46], pybind11::arg>' requested here
cpp_function func(std::forward(f),
^
test_pybind.cpp:42:7: note: in instantiation of function template specialization 'pybind11::module_::def<void ()(const std::shared_ptr<std::vector> &), char[46], pybind11::arg>' requested here
m.def("process_vector", &processVector, "A function that processes a vector of doubles",
^
In file included from test_pybind.cpp:1:
In file included from ./pybind11/include/pybind11/pybind11.h:13:
In file included from ./pybind11/include/pybind11/detail/class.h:12:
In file included from ./pybind11/include/pybind11/attr.h:14:
./pybind11/include/pybind11/cast.h:746:5: error: static_assert failed due to requirement 'std::is_base_of<pybind11::detail::type_caster_base<std::vector<A, std::allocator>>, pybind11::detail::type_caster<std::vector<A, std::allocator>, void>>::value' "Holder classes are only supported for custom types"
static_assert(std::is_base_of<base, type_caster>::value,
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./pybind11/include/pybind11/cast.h:820:48: note: in instantiation of template class 'pybind11::detail::copyable_holder_caster<std::vector, std::shared_ptr<std::vector>>' requested here
class type_caster<std::shared_ptr> : public copyable_holder_caster<T, std::shared_ptr> {};
^
./pybind11/include/pybind11/cast.h:1404:57: note: in instantiation of template class 'pybind11::detail::type_caster<std::shared_ptr<std::vector>>' requested here
static constexpr auto arg_names = concat(type_descr(make_caster::name)...);
^
./pybind11/include/pybind11/pybind11.h:219:34: note: in instantiation of template class 'pybind11::detail::argument_loader<const std::shared_ptr<std::vector> &>' requested here
sizeof...(Args), cast_in::args_pos >= 0, cast_in::has_kwargs),
^
./pybind11/include/pybind11/pybind11.h:92:9: note: in instantiation of function template specialization 'pybind11::cpp_function::initialize<void (&)(const std::shared_ptr<std::vector> &), void, const std::shared_ptr<std::vector> &, pybind11::name, pybind11::scope, pybind11::sibling, char[40], pybind11::arg>' requested here
initialize(f, f, extra...);
^
./pybind11/include/pybind11/pybind11.h:1163:22: note: in instantiation of function template specialization 'pybind11::cpp_function::cpp_function<void, const std::shared_ptr<std::vector> &, pybind11::name, pybind11::scope, pybind11::sibling, char[40], pybind11::arg>' requested here
cpp_function func(std::forward(f),
^
test_pybind.cpp:45:7: note: in instantiation of function template specialization 'pybind11::module_::def<void ()(const std::shared_ptr<std::vector> &), char[40], pybind11::arg>' requested here
m.def("process_vector_of_A", &processVectorofA, "A function that processes a vector of A",
^
In file included from test_pybind.cpp:1:
./pybind11/include/pybind11/pybind11.h:219:41: error: incomplete definition of type 'pybind11::detail::argument_loader<const std::shared_ptr<std::vector> &>'
sizeof...(Args), cast_in::args_pos >= 0, cast_in::has_kwargs),
~~~~~~~^~
./pybind11/include/pybind11/pybind11.h:92:9: note: in instantiation of function template specialization 'pybind11::cpp_function::initialize<void (&)(const std::shared_ptr<std::vector> &), void, const std::shared_ptr<std::vector> &, pybind11::name, pybind11::scope, pybind11::sibling, char[40], pybind11::arg>' requested here
initialize(f, f, extra...);
^
./pybind11/include/pybind11/pybind11.h:1163:22: note: in instantiation of function template specialization 'pybind11::cpp_function::cpp_function<void, const std::shared_ptr<std::vector> &, pybind11::name, pybind11::scope, pybind11::sibling, char[40], pybind11::arg>' requested here
cpp_function func(std::forward(f),
^
test_pybind.cpp:45:7: note: in instantiation of function template specialization 'pybind11::module_::def<void ()(const std::shared_ptr<std::vector> &), char[40], pybind11::arg>' requested here
m.def("process_vector_of_A", &processVectorofA, "A function that processes a vector of A",
^
4 errors generated.
`
Beta Was this translation helpful? Give feedback.
All reactions