Commit bc61d5e2 authored by Robert Bradshaw's avatar Robert Bradshaw

Fix issue with const reference returned results.

This is a bit of a hack, but as Cython doesn't yet understand
const overloads with different (usually const) return types,
it can't provide the right type of reference in this case.
parent 8f1599cd
...@@ -250,7 +250,9 @@ template<typename T> ...@@ -250,7 +250,9 @@ template<typename T>
class __Pyx_FakeReference { class __Pyx_FakeReference {
public: public:
__Pyx_FakeReference() : ptr(NULL) { } __Pyx_FakeReference() : ptr(NULL) { }
__Pyx_FakeReference(T& ref) : ptr(&ref) { } // __Pyx_FakeReference(T& ref) : ptr(&ref) { }
// Const version needed as Cython doesn't know about const overloads (e.g. for stl containers).
__Pyx_FakeReference(const T& ref) : ptr(const_cast<T*>(&ref)) { }
T *operator->() { return ptr; } T *operator->() { return ptr; }
operator T&() { return *ptr; } operator T&() { return *ptr; }
private: private:
......
...@@ -5,6 +5,7 @@ template <typename T> ...@@ -5,6 +5,7 @@ template <typename T>
struct Bar { struct Bar {
Bar & ref() { return *this; } Bar & ref() { return *this; }
const Bar & const_ref() { return *this; } const Bar & const_ref() { return *this; }
const Bar & const_ref_const() const { return *this; }
T value; T value;
}; };
......
# tag: cpp # tag: cpp
from libcpp.vector cimport vector
cdef extern from "cpp_template_ref_args.h": cdef extern from "cpp_template_ref_args.h":
...@@ -9,6 +11,7 @@ cdef extern from "cpp_template_ref_args.h": ...@@ -9,6 +11,7 @@ cdef extern from "cpp_template_ref_args.h":
T value T value
Bar[T] & ref() except + Bar[T] & ref() except +
const Bar[T] & const_ref() except + const Bar[T] & const_ref() except +
const Bar[T] & const_ref_const() except +
cdef cppclass Foo[T]: cdef cppclass Foo[T]:
Foo() Foo()
...@@ -39,3 +42,14 @@ def test_template_ref_attr(int x): ...@@ -39,3 +42,14 @@ def test_template_ref_attr(int x):
cdef Bar[int] bar cdef Bar[int] bar
bar.value = x bar.value = x
return bar.ref().value, bar.const_ref().value return bar.ref().value, bar.const_ref().value
def test_template_ref_const_attr(int x):
"""
>>> test_template_ref_const_attr(4)
4
"""
cdef vector[int] v
v.push_back(x)
cdef const vector[int] *configs = &v
cdef int value = configs.at(0)
return value
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment