[C++-sig] Arrays and Boost.Python
David Abrahams
dave at boost-consulting.com
Sun Mar 30 15:40:57 CEST 2003
Patrick Hartling <patrick at vrac.iastate.edu> writes:
> David Abrahams wrote:
>> Patrick Hartling <patrick at vrac.iastate.edu> writes:
>>
>>>Does Boost.Python (from Boost 1.30.0 or the Boost CVS trunk) provide
>>>handling of arrays as public data members in a C++ class? Using
>>>Boost.Python from Boost 1.30.0, I get an error from GCC 3.2 about line
>>>68 of boost/python/data_members.hpp stating that "ISO C++ forbids
>>>assignment of arrays." If I change the array definition in my
>>>Boost.Python code so that the data member is read-only instead of
>>> read/write, that gets rid of the error.
>> But I doubt it will do anything other than raise an exception when
>> you try to use it from Python.
>
> That is true. I hadn't actually tested it prior to reading your
> response. Oh well.
>
>>>I don't mind doing that in this case--really I am just curious about
>>>how Boost.Python handles C++ arrays.
>> So far, it doesn't. If you can write down precisely what semantics
>> you'd like to see, considering what would constitute an argument match
>> for overloaded functions, we might be able to do something about it.
>
> I hadn't really thought about it much so far. It was one of those
> things I was hoping would "just work." The fact that it is not
> handled right now is not a big problem for me personally.
>
> What I see as a problem is that arrays in C/C++ are just syntactic
> sugar for accessing a block of memory.
That's not quite true. C++ recognizes types like int[4] and
int(&)[4] so the dimensions are part of the type and it doesn't
devolve into a pointer immediately.
> In languages such as Java and C# (and even Managed C++ from what I
> have seen so far), their handling is more formalized. In other
> words, there is no question that a function with a signature such as
> 'void f(int[] intList)' takes an array of integers. In the land of
> C/C++, we have to deal with the uncertainty of 'void f(int*
> intList)'. Is it a pointer to a single integer, an array, or a
> pointer into the middle of an array? All that is certain is that it
> points to four bytes of memory (for ILP32, anyway).
Use boost::array if you can, or you can accept a reference to an
array of a given dimension. Of course, if fixed size isn't what you
want anyway, arrays are the wrong data abstraction.
> Honestly, I don't feel too strongly about seeing support for arrays
> in Boost.Python. Considering that neither lists nor tuples in
> Python provide quite the right semantics for a C-style array, the
> concept may just not map into the world of Python very well. Of
> course, I could be wrong about all of this--I'm just a user, not an
> expert. :)
Ralf Grosse-Kunstleve has done some work which supports mapping
between Python sequences and C++ arrays, vectors, etc. Check the FAQ.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list