C++ allows to create generic constructs by defining templates. For example, if you want to be able to create Point objects with integer as well as floating point coordinates, you could write:
template<typename T>
class Point
{
public:
T get_x();
void set_x(T x);
T get_y();
void set_y(T y);
private:
T x;
T y;
};
This definition creates a generic type in itemis CREATE. In the definition section, you then supply the concrete type parameter, here int32_t:
import: "Point.h"
interface:
var PointA: Point<int32_t>
var PointB: Point<int32_t>
out event e
Instead of int32_t, double, ComplexNumber, or any other type could have been specified instead.
itemis CREATE verifies the usage. Thus with
int32_t, the function call
PointA.set_x(4.2)
would be flagged as an error.
C++ also allows to create template functions, which can, but don’t have to, be a part of a class.
A typical example is a
max(T a, T b)
function:
template<typename T>
T max(T a, T b)
{
return (a < b) ? b : a;
}
Template functions do not have to have their type parameter declared. itemis CREATE checks whether the supplied arguments are compatible. Calling
max(4, 3.5)
would be fine, and
T would be inferred to be
double, whereas
max(4, true) is invalid, because integer types and boolean types are not compatible.