1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# tag: cpp
cimport cython
from cython.operator import dereference as deref
cdef extern from "cpp_templates_helper.h":
cdef cppclass Wrap[T, AltType=*, UndeclarableAltType=*]:
Wrap(T)
void set(T)
T get()
bint operator==(Wrap[T])
AltType get_alt_type()
void set_alt_type(AltType)
UndeclarableAltType create()
bint accept(UndeclarableAltType)
cdef cppclass Pair[T1,T2]:
Pair(T1,T2)
T1 first()
T2 second()
bint operator==(Pair[T1,T2])
bint operator!=(Pair[T1,T2])
cdef cppclass SuperClass[T1, T2]:
pass
cdef cppclass SubClass[T2, T3](SuperClass[T2, T3]):
pass
cdef cppclass Div[T]:
@staticmethod
T half(T value)
def test_int(int x, int y):
"""
>>> test_int(3, 4)
(3, 4, False)
>>> test_int(100, 100)
(100, 100, True)
"""
try:
a = new Wrap[int](x)
b = new Wrap[int](0)
b.set(y)
return a.get(), b.get(), a[0] == b[0]
finally:
del a, b
def test_double(double x, double y):
"""
>>> test_double(3, 3.5)
(3.0, 3.5, False)
>>> test_double(100, 100)
(100.0, 100.0, True)
"""
try:
a = new Wrap[double](x)
b = new Wrap[double](-1)
b.set(y)
return a.get(), b.get(), deref(a) == deref(b)
finally:
del a, b
def test_default_template_arguments(double x):
"""
>>> test_default_template_arguments(3.5)
(3.5, 3.0)
"""
try:
a = new Wrap[double](x)
b = new Wrap[double, int, long](x)
ax = a.get_alt_type()
a.set_alt_type(ax)
assert a.accept(a.create()) # never declared
bx = b.get_alt_type()
b.set_alt_type(bx)
bc = b.create() # declaration here is fine
assert b.accept(bc)
return a.get(), b.get()
finally:
del a
def test_pair(int i, double x):
"""
>>> test_pair(1, 1.5)
(1, 1.5, True, False)
>>> test_pair(2, 2.25)
(2, 2.25, True, False)
"""
try:
pair = new Pair[int, double](i, x)
return pair.first(), pair.second(), deref(pair) == deref(pair), deref(pair) != deref(pair)
finally:
del pair
def test_ptr(int i):
"""
>>> test_ptr(3)
3
>>> test_ptr(5)
5
"""
try:
w = new Wrap[int*](&i)
return deref(w.get())
finally:
del w
cdef double f(double x):
return x*x
def test_func_ptr(double x):
"""
>>> test_func_ptr(3)
9.0
>>> test_func_ptr(-1.5)
2.25
"""
try:
w = new Wrap[double (*)(double)](&f)
return w.get()(x)
finally:
del w
def test_typeof(double x):
"""
>>> test_func_ptr(3)
9.0
>>> test_func_ptr(-1.5)
2.25
"""
try:
w = new Wrap[cython.typeof(&f)](&f)
return w.get()(x)
finally:
del w
def test_cast_template_pointer():
"""
>>> test_cast_template_pointer()
"""
cdef SubClass[int, float] *sub = new SubClass[int, float]()
cdef SuperClass[int, float] *sup
sup = sub
sup = <SubClass[int, float] *> sub
def test_static(x):
"""
>>> test_static(2)
(1, 1.0)
>>> test_static(3)
(1, 1.5)
"""
return Div[int].half(x), Div[double].half(x)
def test_pure_syntax(int i):
"""
>>> test_ptr(3)
3
>>> test_ptr(5)
5
"""
try:
w = new Wrap[cython.pointer(int)](&i)
return deref(w.get())
finally:
del w
|