PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

[FIXED] How to pass python lambda func to c++ std::function<> using Boost.Python

 November 05, 2022     boost, c++, interface, lambda, python     No comments   

Issue

Just lets consider next example:

#include <functional>

class Model
{
  function<bool(const vector<double>&, float, float, float)> q_bifurcate_pointer;
}   

Now in c++ env I can simply assign lambda value to q_bifurcate_pointer:

model.q_bifurcate_pointer = [](const vector<double>& a, float branch_lenght, float bifurcation_threshold, float bifurcation_min_dist)->bool
            {
                return (a.at(2) / a.at(0) <= bifurcation_threshold) 
                    && branch_lenght >= bifurcation_min_dist;
            };

Next I try to export that class to python using 'Boost.Python':

BOOST_PYTHON_MODULE(riversim)
{
class_<vector<double>> ("t_v_double")
  .def(vector_indexing_suite<vector<double>>());

class_<Model>("Model")
  .def_readwrite("q_bifurcate_pointer", &Model::q_bifurcate_pointer)
}

And now we are getting finally to the problem. In python I can execute next lines:

import riversim
model = riversim.Model()
model.q_bifurcate_pointer = lambda a, b, c, d: True

# or more complicated example with type specification
from typing import Callable

func: Callable[[riversim.t_v_double, float, float, float], bool] = lambda a, b, c, d: True
model.q_bifurcate_pointer = func

And in both cases I am getting next error:

---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
/home/oleg/Documents/riversim/riversimpy/histogram.ipynb Cell 2' in <module>
      3 model = riversim.Model()
      4 func: Callable[[riversim.t_v_double, float, float, float], bool] = lambda a, b, c, d: True
----> 5 model.q_bifurcate_pointer = func

ArgumentError: Python argument types in
    None.None(Model, function)
did not match C++ signature:
    None(River::Model {lvalue}, std::function<bool (std::vector<double, std::allocator<double> > const&, float, float, float)>)

So, how to create proper lambda function and past it to my c++ code?


Solution

Lambda expression in pythone - this is byte code and C++ needs machine code. Just googled similar to mine but more general problem here: https://stackoverflow.com/a/30445958/4437603



Answered By - Oleg Kmechak
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing