Commit 1f98bcd7 authored by Robert Bradshaw's avatar Robert Bradshaw

Support curiously recurring template pattern.

Closes #1458.
parent 7d763702
......@@ -3275,7 +3275,7 @@ class CppClassType(CType):
self.templates = templates
self.template_type = template_type
self.num_optional_templates = sum(is_optional_template_param(T) for T in templates or ())
self.specializations = {}
self.specializations = {tuple(zip(templates, templates)): self}
self.is_cpp_string = cname in cpp_string_conversions
def use_conversion_utility(self, from_or_to):
......
# tag: cpp
cdef extern from "curiously_recurring_template_pattern_GH1458_suport.h":
cdef cppclass Base[T, Derived]:
Base(T)
Derived half()
T calculate()
cdef cppclass Square[T](Base[T, Square[T]]):
Square(T)
cdef cppclass Cube[T](Base[T, Cube[T]]):
Cube(T)
def test_derived(int x):
"""
>>> test_derived(5)
(6.25, 8)
"""
try:
square_double = new Square[double](x)
cube_int = new Cube[int](x)
return square_double.half().calculate(), cube_int.half().calculate()
finally:
del square_double, cube_int
template<typename T, class Derived>
class Base {
public:
Base(T x) : x_(x) { };
Derived half() {
Derived d(x_ / 2);
return d;
};
virtual T calculate() = 0;
virtual ~Base() { };
protected:
T x_;
};
template<typename T>
class Square : public Base<T, Square<T> > {
public:
Square(T x) : Base<T, Square<T> >(x) { };
T calculate() { return this->x_ * this->x_; }
};
template<typename T>
class Cube : public Base<T, Cube<T> > {
public:
Cube(T x) : Base<T, Cube<T> >(x) { };
T calculate() { return this->x_ * this->x_ * this->x_; }
};
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