Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using c++11 lambda as accessor function in boost::python's add_property (get_signature fails with lambda)

I am trying to use c++11 lambdas as accessor functions in boost::python's add_property, something along the following (the lambda is not strictly needed in this example, but will be needed for more complicated things happening inside the lambda, such as validation):

#include<boost/python.hpp>

struct A{
  A(): a(2){};
  int a;
};

BOOST_PYTHON_MODULE(boost_python_lambda)
{
  boost::python::class_<A>("A")
    // .def_readonly("a",&A::a) // the classical way: works fine 
    .add_property("a",[](const A& a){return a.a;})
  ;
}

However, compiling with clang++ (ver. 3.2) and -std=c++11 (the result is the same with g++ 4.7), I get this error:

/usr/include/boost/python/class.hpp:442:66: error: no matching function for call to 'get_signature'
        return python::make_function(f, default_call_policies(), detail::get_signature(f, (T*)0));
                                                                 ^~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/python/class.hpp:422:22: note: in instantiation of function template specialization 'boost::python::class_<A,
      boost::python::detail::not_specified, boost::python::detail::not_specified,
      boost::python::detail::not_specified>::make_fn_impl<A, <lambda at boost_python_lambda.cpp:12:21> >' requested here
        return this->make_fn_impl(
                     ^
/usr/include/boost/python/class.hpp:309:40: note: in instantiation of function template specialization 'boost::python::class_<A,
      boost::python::detail::not_specified, boost::python::detail::not_specified,
      boost::python::detail::not_specified>::make_getter<<lambda at boost_python_lambda.cpp:12:21> >' requested here
        base::add_property(name, this->make_getter(fget), docstr);
                                       ^
boost_python_lambda.cpp:12:4: note: in instantiation of function template specialization 'boost::python::class_<A,
      boost::python::detail::not_specified, boost::python::detail::not_specified,
      boost::python::detail::not_specified>::add_property<<lambda at boost_python_lambda.cpp:12:21> >' requested here
                .add_property("a",[](const A& a){return a.a;})
                 ^

I tried wrapping the lambda in std::function<int(const A&)>(...), but that did not help with the argument deduction. Any idea?

like image 302
eudoxos Avatar asked May 30 '13 20:05

eudoxos


2 Answers

Hopping in here two years later, Boost.Python indeed does not support wrapping function objects. But your lambda does not capture anything. As such, it can be explicitly convertible to a function pointer:

BOOST_PYTHON_MODULE(boost_python_lambda)
{
  boost::python::class_<A>("A")
    // .def_readonly("a",&A::a) // the classical way: works fine 
    .add_property("a", +[](const A& a){return a.a;})
                       ↑↑↑
  ;
}

All you need is that +.

like image 88
Barry Avatar answered Nov 28 '22 22:11

Barry


Use the make_function() function to create Python callable objects. If Boost.Python cannot deduce the function object signature, then the signature must be explicitly provided as an MPL front-extensible sequence. For example, the lambda [](const A& a) { return a.a; } returns an int and accepts const A&, so one could use boost::mpl::vector<int, const A&>() for the signature.

Here is a complete example demonstrating using a pointer-to-data-member, casting non-capturing lambda to a function, and using a lambda/functor:

#include <boost/python.hpp>

struct A
{
  A(): a(2) {};
  int a;
};

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<A>("A")
    // Expose pointer-to-data-member.
    .add_property("a1", &A::a)
    // Cast non-capturing lambda to a function.
    .add_property("a2", +[](const A& a) { return a.a; })
    // Make a python function from a functor.
    .add_property("a3", python::make_function(
        [](const A& a) { return a.a; },
        python::default_call_policies(),
        boost::mpl::vector<int, const A&>()))
    ;
}

Interactive usage:

>>> import example
>>> a = example.A()
>>> assert(a.a1 == 2)
>>> assert(a.a2 == 2)
>>> assert(a.a3 == 2)

An alternative non-intrusive approach that is based on documented behavior is to write the lambda as a non-member function, then exposing it as the fget argument. While not as succinct as lambda, it still allows for additional functionality, such as validation, to occur when accessing the member variable.

#include <boost/python.hpp>

struct A{
  A(): a(2){};
  int a;
};

int get_a(const A& a)
{
  // do validation
  // do more complicated things
  return a.a;
}

BOOST_PYTHON_MODULE(example)
{
  boost::python::class_<A>("A")
    .add_property("a", &get_a);
  ;
}
like image 31
Tanner Sansbury Avatar answered Nov 28 '22 21:11

Tanner Sansbury