Class templates
Here we demonstrate how to generate Python bindings class templates.
In contrast to Function templates, class templates need to be instanstiated in the special
c2py_module namespace with a using declaration.
#include "c2py/c2py.hpp"
#include <string>
// A class template.
template <typename T> struct adder {
T a;
adder(T a) : a(a) {}
T add(T b) { return a + b; }
};
namespace c2py_module {
// Using declarations to instantiate the class template for int, double and std::string.
using int_adder = adder<int>;
using double_adder = adder<double>;
using string_adder = adder<std::string>;
} // namespace c2py_module
clair performs the actual instantiation for us and gives it the desired Python name written on
the left-hand side of the using declaration.
After creating the Python bindings,
clair-c2py class_template.cpp -- -std=c++20 `c2py_flags -i`
clang++ class_template.cpp -std=c++20 -shared -o class_template.so `c2py_flags`
we can test the extension module in Python:
>>> from class_template import *
>>> a_i = int_adder(2)
>>> a_i.add(5)
7
>>> a_d = double_adder(3.14)
>>> a_d.add(5.7)
8.84
>>> a_s = string_adder("Hello ,")
>>> a_s.add("clair!")
'Hello ,clair!'