[C++-sig] Re: C arrays mk 2

David Abrahams dave at boost-consulting.com
Thu Oct 16 00:01:26 CEST 2003


Raoul Gough wrote:
> David Abrahams <dave at boost-consulting.com> writes:
> 
> 
>>Raoul Gough wrote:
> 
> [snip]
> 
>>>I suppose it depends how rich an interface he wants on the Python
>>>side. The two methods you named are sufficient for a lot of cases
>>>(almost everything, in fact), but if he wants slice support or
>>>__setitem__, it might still pay off to use the container suite.
>>
>>Sure.  But his example just creates an iterator (the extremely hard
>>way), and __len__ + __getitem__ already gets you an iterator and more.
> 
> 
> That's right - it's neat how many Python expressions can work with
> just those two methods (in fact, I think __len__ is not even necessary
> for much of it, since IndexError works as well as StopIteration).
> However, I think his code actually does enlist some extra
> container_suite support via the iterator_pair container emulation,
> which goes one or two steps further. The macros obscure it somewhat,
> but I think it basically does this:
> 
> typedef indexing::iterator_pair<memberType *> IterPair;
> 
> class_<IterPair>("Name", init<...>)
>   .def (indexing::container_suite<IterPair>());

Ah, I didn't realize he was using the indexing suite.  Sorry!
I think I have some arguments with the naming of iterator_pair, which is 
part of what threw me off.
It really represents an "immutable sequence".  "iterator pair" is more 
like an implementation detail.

As a side note, please use boost::detail::iterator_traits instead of 
std::iterator_traits, for portability reasons.

As another side note, endline layout

   template<typename Iterator>
   iterator_pair<Iterator>::iterator_pair (iterator_param begin
                                           , iterator_param end)

is banned in Boost.Python.  Should be, e.g.:

   template<typename Iterator>
   iterator_pair<Iterator>::iterator_pair (
       iterator_param begin, iterator_param end)

;->

I'll stop picking nits with your excellent work now.

> Which usually gives you __getitem__ and __setitem__ with slice
> support, as well as __len__, index, count, sort and reverse. Now
> whether you actually need all that stuff is another question!

Indeed.







More information about the Cplusplus-sig mailing list