How to extend a templated c++ class in python with SWIG to allow the [] operator

后端 未结 3 815
栀梦
栀梦 2020-12-19 10:17

I have a templated c++ array class which uses the standard vector class:

#include 
#include 

using namespace std;

template

        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 10:42

    To provide a more generic solution:

    %define ArrayExtendVal(name, T) %extend name { T getitem(int i) { return (*self)[i]; } } %enddef %define ArrayExtendRef(name, T) %extend name { T& getitem(int i) { return (*self)[i]; } } %enddef %define ArrayExtendConstRef(name, T) %extend name { const T& getitem(int i) { return (*self)[i]; } } %enddef ... %ignore myNamespace::myClass::operator[] %include "my_class.h" ArrayExtendVal(myClass, double); ArrayExtendRef(myClass, double); ArrayExtendConstRef(myClass, double);

    Note the %ignore directive that is missing in the other answers.

提交回复
热议问题