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>
class __Pyx_FakeReference {
public:
__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; }
operator T&() { return *ptr; }
private:
......
......@@ -5,6 +5,7 @@ template <typename T>
struct Bar {
Bar & ref() { return *this; }
const Bar & const_ref() { return *this; }
const Bar & const_ref_const() const { return *this; }
T value;
};
......
# tag: cpp
from libcpp.vector cimport vector
cdef extern from "cpp_template_ref_args.h":
......@@ -9,6 +11,7 @@ cdef extern from "cpp_template_ref_args.h":
T value
Bar[T] & ref() except +
const Bar[T] & const_ref() except +
const Bar[T] & const_ref_const() except +
cdef cppclass Foo[T]:
Foo()
......@@ -39,3 +42,14 @@ def test_template_ref_attr(int x):
cdef Bar[int] bar
bar.value = x
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