[C++-sig] Question about passing ordinary pointers (like int*) to functions

Roman Yakovenko roman.yakovenko at gmail.com
Fri Oct 20 11:31:01 CEST 2006


On 10/20/06, Shandong Han <shandong.han at mizuho-sc.com> wrote:
> Sorry if this is too naive, but I searched the internet and digged out the
> previous mails here without any luck...
>
> I am new to this, and my question is pretty simple: how to call functions
> which use a pointer to return value? Sample code is as follows:
>
> ===================================
>
> #include "boost/python.hpp"
> #include "boost/python/suite/indexing/vector_indexing_suite.hpp"
>
> namespace bp = boost::python;
>
> int add(int a, int b, int* s)
> {
>         *s = a + b;
>         return 0;
> }
>
> BOOST_PYTHON_MODULE(hello){
> //    bp::def("add", &::add);
>
>         { //::add -- generated using py++
>                 typedef int ( *function_ptr_t )( int,int,int * );
>
>                 bp::def(
>                         "add"
>                         , function_ptr_t( &::add )
>                         , ( bp::arg("a"), bp::arg("b"), bp::arg("s") )
>                         , bp::default_call_policies() );
>                 }
> }
>
> ===================================
>
> It builds fine. So test it in python:
>
> >>> from ctypes import *
> >>> from hello import *
> >>> a=c_int()
> >>> add(1,1,byref(a))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> Boost.Python.ArgumentError: Python argument types in
>     hello.add(int, int, CArgObject)
> did not match C++ signature:
>     add(int a, int b, int * s)
> >>>
> >>> add(1,1,pointer(a))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> Boost.Python.ArgumentError: Python argument types in
>     hello.add(int, int, LP_c_long)
> did not match C++ signature:
>     add(int a, int b, int * s)
>
> Maybe the use of ctypes is not the correct way, but I don't know how I can
> make a pointer without using that... Could you let me know how to do this?
>
> And another question about arrays:
>
> int arrayTest(int n, const int a[], int b[])
> {
>         for(int i = 0; i < n; i++)
>                 b[i] = a[i] * 2;
>         return 0;
> }
>
> py++ treats the arrays as pointers:
>
> typedef ::std::string ( *function_ptr_t )( ::std::string const & );
>
>         bp::def(
>             "myFunc"
>             , function_ptr_t( &::myFunc )
>             , ( bp::arg("arg0") )
>             , bp::default_call_policies() );
>
> So this becomes the same question as the above one...

C++ treats arrays as pointers, not Py++. Any way you have a problem, which
should be solved. David Abrahams already provided you with solution, I
will point
you to experimental code in Py++ that will allow you to implement the solution
with it.

We call the functionality you are looking for - "Function Transformation".
It wil allow you to instruct Py++ to create right function wrapper.

Start here: https://realityforge.vrsource.org/view/PyppApi/CodeInserter
Take a look on Py++ unit tests:
    http://tinyurl.com/uo5kh  - usage example
    http://tinyurl.com/vv9j6  - C++ source code to expose

This feature presents in Py++-0.8.2 release. The feature is highly
experimental,
nevertheless very useful. The API will change. Can you try it and provide
some feedback?

Thanks

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list