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

Nicodemus nicodemus at globalite.com.br
Sat Jun 14 01:43:58 CEST 2003


Mike Rovner wrote:

>"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.
>  
>

You are right, thanks for the remainder!

Regards,
Nicodemus.





More information about the Cplusplus-sig mailing list