Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Cannot compile template class with rebinding function #6156

Open
panda-34 opened this issue Apr 23, 2024 · 0 comments
Open

[BUG] Cannot compile template class with rebinding function #6156

panda-34 opened this issue Apr 23, 2024 · 0 comments

Comments

@panda-34
Copy link

Describe the bug

This code compiles:

cdef extern from "vectors.hpp":
	cdef cppclass Vector[T]:
		Vector()
		Vector[T] operator-(T other)
		Vector[U] cast[U]()

def test():
	Vector[float]() - <float>1.0 # always works
	Vector[int]().cast[float]() - <float>1.0 # works

But if you just swap two lines declaring operator- and cast:

cdef extern from "vectors.hpp":
	cdef cppclass Vector[T]:
		Vector()
		Vector[U] cast[U]()
		Vector[T] operator-(T other)

def test():
	Vector[float]() - <float>1.0 # always works
	Vector[int]().cast[float]() - <float>1.0 # error

It throws an error: Invalid operand types for '-' (Vector[float]; float)

In the second expression cython internally uses Vector[float] type which is different from Vector[float] type in the first expression

Code to reproduce the behaviour:

No response

Expected behaviour

Order of declaration should not matter (it does not matter for equivalent C++ code):

template <typename T>
class Vector {
public:
	template <typename U>
	Vector<U> cast() { return Vector<U>(); }
	Vector operator-(T other) { return Vector(); }
};

void test() {
	Vector<int>().cast<float>() - 1.0f;
}

OS

No response

Python version

3.10.5

Cython version

3.0.10

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants