Using std::unique_ptr<std::string> return type #909
Answered
by
wjakob
romanfomin
asked this question in
Q&A
-
This is simplified example where I add python module with function returning #include <nanobind/nanobind.h>
namespace nb = nanobind;
std::unique_ptr<std::string> Create()
{
return std::make_unique<std::string>("qwerty");
}
NB_MODULE(mynb, m)
{
m.def("create", []() { return Create(); });
} When trying to call this function in python I get error: >>> import mynb
>>> mynb.create()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Unable to convert function return value to a Python type! The signature was
create() -> std::unique_ptr<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::default_delete<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >
>>> How to fix it? I tried to add |
Beta Was this translation helpful? Give feedback.
Answered by
wjakob
Feb 3, 2025
Replies: 1 comment 5 replies
-
Did you include |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The first error message
already has all the information. You cannot mix
unique_ptr
and types handled by casting (std::string
) in this way. It's not supported and will not be supported in the future.