I want to wrap a C++ struct which has a comparison operator using Cython:
struct myStruct {
float a;
float b;
float c;
float d;
bool operator==(const myStruct &myStr) const = default;
};
I am struggling to figure out how to perform the operator overload in Cython.
cdef struct myStruct:
np.float32_t a;
np.float32_t b;
np.float32_t c;
np.float32_t d;
# Operator overload???
I did not specify the operator overload in the cython definition of the struct, and the build succeeded. I thought it would fail with an error.
Cython only cares that the comparison exists. It doesn't care how the comparison is generated. Therefore just don't tell it about the
= defaultThe only consequence of not telling Cython is that you won't be able to use the operator in Cython.
You're missing two things. First operators can only be attached to
cppclassand notstructso change:to
It really doesn't matter from Cython's point of view whether it's actually defined with
classorstruct- this only affects the default accessibility within C++.Second, Cython doesn't know about
boolby default. Either changebooltobint(binary-int) or addfrom libcpp cimport bool