Skip to content

Defining a custom string class #4685

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

You must be logged in to vote

The answer really always comes up just after asking in a forum.
What does the trick for me:

namespace pybind11::detail {
    template <> struct type_caster<String> : public type_caster_base<String> {
        using base = type_caster_base<String>;
    public:
        bool load(handle src, bool convert) {
            if (base::load(src, convert)) {
                return true;
            }
            else if (py::isinstance<py::str>(src)) {
                value = new String(py::cast<std::string>(src));
                return true;
            }
            return false;
        }

        static handle cast(const String& src, return_value_policy policy, handle parent) {
            return 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by HerrNamenlos123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant