Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:02:57+00:00 2026-05-23T22:02:57+00:00

So, I am trying to create a to_python converter that will allow me to

  • 0

So, I am trying to create a to_python converter that will allow me to return a boost::optional from an exposed function and have it treated as T if the optional is set and None if not. Based on a post I found on C++Sig, I wrote the following code.

template<typename T>
struct optional_ : private boost::noncopyable {
  struct conversion {
    static PyObject* convert(boost::optional<T> const& value) {
      if (value) {
        return boost::python::to_python_value<T>()(*value);
      }
      Py_INCREF(Py_None);
      return Py_None;
    }
  };
  explicit optional_() {
    boost::python::to_python_converter<boost::optional<T>, conversion>();
  }
};

As far as I can tell, it works for converting the optionals, but python throws the following exception “TypeError: No to_python (by-value) converter found for C++ type: std::string”. I know that C++ is able to convert strings to python since most of my exposed functions return strings. Why doesn’t boost::python::to_python_value recognize it, and how can I utilize whatever converter it has?

Fixed by changing to the following (based on this article):

template<typename T>
struct optional_ : private boost::noncopyable {
  struct conversion {
    static PyObject* convert(boost::optional<T> const& value) {
      using namespace boost::python;
      return incref((value ? object(*value) : object()).ptr());
    }
  };
  explicit optional_() {
    boost::python::to_python_converter<boost::optional<T>, conversion>();
  }
};

Now to just do the other version so that it is cleaner and works better.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:02:57+00:00Added an answer on May 23, 2026 at 10:02 pm

    Ok here is the entire to and from optional converter based on the original C++ sig post but rewritten to use the high level boost.python API (sorry about the weird spacing).

    template<typename T>
    struct optional_ : private boost::noncopyable
    {
      struct conversion :
        public boost::python::converter::expected_from_python_type<T>
      {
        static PyObject* convert(boost::optional<T> const& value) {
          using namespace boost::python;
          return incref((value ? object(*value) : object()).ptr());
        }
      };
    
      static void* convertible(PyObject *obj) {
        using namespace boost::python;
        return obj == Py_None || extract<T>(obj).check() ? obj : NULL;
      }
    
      static void constructor(PyObject *obj,
           boost::python::converter::rvalue_from_python_stage1_data *data)
      {
        using namespace boost::python;
        void *const storage =
          reinterpret_cast<
            converter::rvalue_from_python_storage<boost::optional<T> >*
          >(data)->storage.bytes;
        if(obj == Py_None) {
          new (storage) boost::optional<T>();
        } else {
          new (storage) boost::optional<T>(extract<T>(obj));
        }
        data->convertible = storage;
      }
    
      explicit optional_() {
        using namespace boost::python;
        if(!extract<boost::optional<T> >(object()).check()) {
          to_python_converter<boost::optional<T>, conversion, true>();
          converter::registry::push_back(
            &convertible,
            &constructor,
            type_id<boost::optional<T> >(),
            &conversion::get_pytype
          );
        }
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a python server that will serve calls from outer source
I'm trying to create a python script that will allow me to load up
I have a python script that is trying to create a directory tree dynamically
I'm trying to write something that will allow a user to use a keyboard/mouse
Update I'm trying to create a simple Go function which will simply take in
I am trying to create a function which will load a whole lot of
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
Trying to create a list to return some JSON data to a view. Following
I'm trying to create a self-contained version of pisa (html to pdf converter, latest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.