Skip to content

No std::string -> nanobind::str conversion? #309

Closed Answered by wjakob
nschloe asked this question in Q&A
Discussion options

You must be logged in to vote

nb::str has an explicit conversion from char * (but not an implicit one!). You must cast it like so: nb::str(std_str.c_str()). There is no way that there could be a nb::str constructor that takes std::string — nanobind tries as hard as possible to not include any unnecessary STL headers.

What I would suggest is that you make your own nb::str subclass (nschloe_str ;)) with all the implicit and explicit constructors you fancy. Since it derives from a builtin wrapper type, nanobind knows how to accept and return it in bindings.

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@nschloe
Comment options

@wjakob
Comment options

@nschloe
Comment options

Answer selected by nschloe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants