1# tag: cpp
2
3from libcpp.vector cimport vector
4
5
6cdef extern from "cpp_template_ref_args.h":
7
8    cdef cppclass Bar[T]:
9        Bar()
10        # bug: Bar[T] created before class fully defined
11        T value
12        Bar[T] & ref() except +
13        const Bar[T] & const_ref() except +
14        const Bar[T] & const_ref_const() except +
15
16    cdef cppclass Foo[T]:
17        Foo()
18        int bar_value(Bar[int] & bar)
19
20
21def test_template_ref_arg(int x):
22    """
23    >>> test_template_ref_arg(4)
24    4
25    """
26
27    # Templated reference parameters in method
28    # of templated classes were not properly coalesced.
29
30    cdef Foo[size_t] foo
31    cdef Bar[int] bar
32
33    bar.value = x
34
35    return foo.bar_value(bar.ref())
36
37def test_template_ref_attr(int x):
38    """
39    >>> test_template_ref_attr(4)
40    (4, 4)
41    """
42    cdef Bar[int] bar
43    bar.value = x
44    return bar.ref().value, bar.const_ref().value
45
46def test_template_ref_const_attr(int x):
47    """
48    >>> test_template_ref_const_attr(4)
49    4
50    """
51    cdef vector[int] v
52    v.push_back(x)
53    cdef const vector[int] *configs = &v
54    cdef int value = configs.at(0)
55    return value
56