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 6824237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:52:40+00:00 2026-05-26T21:52:40+00:00

Ive been working with Python and C++ together for some time, but never tried

  • 0

Ive been working with Python and C++ together for some time, but never tried to implement what follows:

Id like the python user to be able to write something like:

def foo(a,b):
    return a+b

myclass.myfunc(foo)

where myclass is a c++ class exposed to python with Boost.Python, with one of its methods (myfunc) that takes a function with:

int func(int,int)

signature, and only that.

Is this possible?

Im thinking about declaring:

myclass::myfunc(boost::python::object)

and extracting the typedef’ed function signature, but im just guessing..

maybe there’s a better/feasible way to do this, maybe with some ‘function’ object?

  • 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-26T21:52:41+00:00Added an answer on May 26, 2026 at 9:52 pm

    You pretty much guessed the answer. Python functions are indeed just boost::python::object instances. You can then just have a boost::function<int (int, int)> and put the Python object inside that.

    I just installed my OS and I don’t have Boost yet, so I can’t test it, but I think it will work if you just do this (without any wrapper functions):

    void function(boost::function<int (int, int)> func) {
        // ...
    }
    
    // And then you expose the function as you normally would
    

    I expect the above to work; if it doesn’t this surely will:

    void function_wrap(boost::python::object func)
    {
        auto lambda = [func](int a, int b) -> int {
            return boost::python::extract<int>(func(a, b));
        };
        function(boost::function<int (int, int)>(lambda));
    }
    
    // And then you expose the wrapper, not the original function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ive been using MSVC2010 for some time now combined with Python Tools 1.0 I'm
I've been working with Boost::Python to expose some high-performance code to python recently, and
I've been working on this for some time now and I can't figure out
I had a question about boost python. I've been working on exporting some functionality
I've been a long time user of R and have recently started working with
So far, I've been working only with python manage.py celeryd , starting it like
I've got a Python/GTK project I've been working on for a while, and some
I am primarily a Python programmer, but I've been working with C because Python
I've been working with python for a while now and am just starting to
In my efforts to resolve Python issue 1578269 , I've been working on trying

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.