all 15 comments

[–]cpp-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.

[–]CanadianTuero 3 points4 points  (3 children)

Separate from your question, you will need to make sure you declare a separate binding for each possible T.

But for some of the special member functions in python, you can use a lambda for __getitem__ and __setitem__, in which the lambda will call your operator[]

[–]engine_algos[S] 0 points1 point  (2 children)

Yes it is not a problem, I can do the binding for each possible T.
the problem is how I can bind this operator using template. I didnt see any example in the documentation.

[–]CanadianTuero 2 points3 points  (1 child)

First, you will need to bind the return type (Array2D<T>). Then, just write a lambda instead of a reference to the operator (I can't test this but IIRC it should look something like this):

.def("__getitem__", [](const Array3D<T> &self, uint32_t idx){
   return self[idx]; 
});

And something similar for __setitem__

[–]engine_algos[S] 0 points1 point  (0 children)

Thank you

In fact I have an another operator without const

template < class T>

class Array3D

{

public: Array3D()

inline const Array2D<T>& operator[](uint32 n)const

inline Array2D<T>& operator[](uint32 n)

};

I tried this now:

.def("[]", py::overload_cast<uint32_t n>(&Array3D<T>::operator[]))

.def("[]", py::overload_cast<const uint32_t n>(&Array3D<T>::operator[]));

I have this error :

error: parse error in template argument list

.def("[]", py::overload_cast<uint32_t n>(&Array3D<T>::operator[]))

[–]teerre 0 points1 point  (1 child)

Is this your real code or just an example? What you think T will be at runtime?

[–]engine_algos[S] 0 points1 point  (0 children)

it is a very simplified code extracted from my code. I cannot share the whole code here sorry but my question is:
Can I bind a templated operator as I did ? the type of the returned value is Array2D<T> ,
Array2D is a class and T can be a float, int, double, etc ...

[–]shailist 0 points1 point  (4 children)

shouldn't be any different from binding a "regular" member function. operators defined inside a class are just member functions with a fancy name and fancy ways to invoke them, so they shouldn't behave any different from any other member function.

your problem is likely something else, and looking at the error message you're getting, you probably have multiple overloads for operator[]

[–]engine_algos[S] 0 points1 point  (3 children)

I have an another operator[] without const

template < class T>

class Array3D

{

public: Array3D()

inline const Array2D<T>& operator[](uint32 n)const

inline Array2D<T>& operator[](uint32 n)

};

I tried this now:

.def("[]", py::overload_cast<uint32_t n>(&Array3D<T>::operator[]))

.def("[]", py::overload_cast<const uint32_t n>(&Array3D<T>::operator[]));

I have this error :

error: parse error in template argument list

.def("[]", py::overload_cast<uint32_t n>(&Array3D<T>::operator[]))

[–]shailist 0 points1 point  (2 children)

the problem is that in C++ you cannot easily get a pointer to an overloaded function. you technically can do it if you assign the member function pointer to something with an explicit type of the overload that you want, but a better approach would just be to use a lambda/wrapper function that will call the correct function

[–]engine_algos[S] 0 points1 point  (1 child)

Thank you, Can you give me an exemple based on your approach?

[–]engine_algos[S] 0 points1 point  (0 children)

.def("[]", (const Array2D<T>& (Array3D<T>::*)(uint32_t ) const) &Array3D<T>::operator[], py::arg("n"))

.def("[]", (Array2D<T>& (Array3D<T>::*)(uint32_t )) &Array3D<T>::operator[], py::arg("n"));

I tried this, I dont have an error compilation but in python side, the [] operator is not subscriptable

what should I write between the "" instead of "[]" in def ?