[C++-sig] Re: Howto wrap operator[]? (boost::python)

Mike Rovner mike at bindkey.com
Sat Jun 14 01:30:55 CEST 2003


"Nicodemus" <nicodemus at globalite.com.br> wrote in message
news:3EEA3FE1.3080601 at globalite.com.br...

> template <class T>
> void vector_setitem(std::vector<T>& v, int index, T value)
> {
>     if (index >= 0 && index < v.size()) {
>         v[index] = value;
>     }
>     else {
>         PyErr_SetString(PyExc_IndexError, "index out of range");
>         throw_error_already_set();
>     }
> }

That will forbid very useful Python feature - negative index :(
So better include

     if( index < 0 ) index+=v.size();

before your if.

Regards,
Mike







More information about the Cplusplus-sig mailing list