разница есть между array<float,3> и float
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/array.hpp>
const int NC=6;
using namespace boost::python;
typedef boost::array<float,NC> Tarr6;
template<class T, int N>
struct _arrayN
{
typedef boost::array<T,N> arrayN;
static T get(arrayN const& self, int idx)
{
if( 0<=idx && idx<N ) return self[idx];
PyErr_SetString(PyExc_KeyError,"index out of range");
throw_error_already_set();
}
static boost::python::list getslice(arrayN const& self, int a,int b)
{
if( !(0<=a<N && 0<b<=N) )
{
PyErr_SetString(PyExc_KeyError,"index out of range");
throw_error_already_set();
}
if(b>N){b=N;}
if(a<0){a=0;}
boost::python::list t;
for(int i=a;i<b;++i)
t.append(self[i]);
return t;
}
static void setslice(arrayN& self, int a,int b,boost::python::object& v)
{
if( !(0<=a<N && 0<b<=N) )
{
PyErr_SetString(PyExc_KeyError,"index out of range");
throw_error_already_set();
}
if(b>N){b=N;}
if(a<0){a=0;}
for(int i=a;i<b;++i)
self[i]=extract<float>(v[i]);
}
static void set(arrayN& self, int idx, const T val) { self[idx]=val; }
static boost::python::list values(arrayN const& self)
{
boost::python::list t;
for(int i=0;i<N;++i)
t.append(self[i]);
return t;
}
static int size(arrayN const & self)
{
return NC;
}
};
BOOST_PYTHON_MODULE(rbt10)
{
class_<Tarr6>("Tarr6")
.def("__len__", &_arrayN<float,NC>::size)
// .def("__getitem__",&_arrayN<float,NC>::get,return_value_policy<copy_non_const_reference>())
.def("__getitem__",&_arrayN<float,NC>::get)
.def("__getslice__",&_arrayN<float,NC>::getslice)
.def("__setslice__",&_arrayN<float,NC>::setslice)
.def("__setitem__",&_arrayN<float,NC>::set)
.def("values", &_arrayN<float,NC>::values)
;
}
Всетаки пришлось мусор порезать, надеюсь будет компилироваться. (msvc10,Boost 1.47.0)