I have a struct that contains a C-style array data member. I'd like to have
this struct exposed to Python, and this data member be accessible as a tuple
in Python.

struct S
{
  char arr[10];
};

void foo( S const * )
{}

BOOST_PYTHON_MODULE( test )
{
  using namespace boost::python;

  class_<S>( "S" )
    .def_readwrite( "arr", &S::arr )
    ;

  def( "foo", foo );
}

The code above fails to build

error C2440: '=' : cannot convert from 'const char [10]' to 'char [10]'

C-style arrays are not assignable, so the error makes sense. The code
compiles if I change the data member to a plain char instead of an array.

I cannot replace the array with an std::array or some other container
because the structure is being consumed by a C API. The only solution I can
think of is to write a couple of wrappers and do the following

    1. A struct S1 that duplicates S except it'll have an std::array instead
of a C-style array
    2. A foo_wrapper that accepts a S1 const *, copies the contents over to
an S instance and calls foo
    3. Register a to_python_converter to convert the std::array to a Python
tuple

This should work, and I'm not too concerned about the data copying at this
point, but it'd be nice if I could avoid it and expose the array directly
without having to jump through all these hoops.

So the question is, how can I expose that C-style array data member to
Python as a tuple?

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to