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
|
discard """
targets: "cpp"
output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest\n[0, 0, 20, 0]\n4"
"""
{.emit: """/*TYPESECTION*/
template <int N, class T>
struct GenericIntType {
T data[N];
};
template <class T>
struct GenericTType {
T field;
};
struct SimpleStruct {
int field;
};
""" .}
type
GenericIntType[N: static[int]; T] {.importcpp: "GenericIntType<'0, '1>".} = object
data: array[N, T]
GenericIntTypeAlt[N: static[int]; T] {.importcpp: "GenericIntType".} = object
GenericTType[T] {.importcpp: "GenericTType<'0>".} = object
field: T
GenInt4 = GenericIntType[4, int]
SimpleStruct {.importcpp: "SimpleStruct"} = object
field: int
var
a = GenInt4()
b = SimpleStruct()
c = GenericTType[float]()
d = SimpleStruct(field: 15)
e = GenericTType[string](field: "test")
f = GenericIntTypeAlt[4, int8]()
a.data[2] = 10
b.field = 5
c.field = 1.2
echo a.data
echo b.field
echo c.field
echo d.field
echo e.field
proc plus(a, b: GenInt4): GenInt4 =
for i in 0 ..< result.data.len:
result.data[i] = a.data[i] + b.data[i]
echo plus(a, a).data
echo sizeof(f)
|