[C++-sig] Passing C++ arrays as arguments

David Abrahams dave at boost-consulting.com
Mon Nov 25 03:52:42 CET 2002

Kerim Borchaev <warkid at hotbox.ru> writes:

> Hello.
> I'm exposing to Python some third party library wich has an API like
> this:
> struct Abstract{
>     virtual void f(float[16])=0;

Well, this function signature is really, truly equivalent to 

      virtual void f(float*) = 0;

So Boost.Python can't tell anything about the size of that array. It
just sees a float* (unless you're using Visual C++, which broken-ly
makes the argument type different from float*... but Boost.Python
doesn't do anything special with the information in that case).

> };
> Abstract *createInstance();
> with this code:
> def("createInstance", createInstance, return_value_policy<manage_new_object>());
> class_<Abstract, boost::noncopyable>("Abstract", no_init)
>     .def("f", Abstract::f);


> Can I simply define some from python tuple to float[16] converter so
> that I can call object returned from createInstance like this:
> createInstance().f((0,)*16) ?

Nope. That's the problem with your function signature. It doesn't
capture any information about the number of elements. I suggest you
look at Ralf Grosse-Kunstleve's work described in the Boost.Python
FAQ, and use boost::array<float,16> if you can.


                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

More information about the Cplusplus-sig mailing list