tstaticimportcpp.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. discard """
  2. targets: "cpp"
  3. output: "[0, 0, 10, 0]\n5\n1.2\n15\ntest\n[0, 0, 20, 0]\n4"
  4. """
  5. {.emit: """/*TYPESECTION*/
  6. template <int N, class T>
  7. struct GenericIntType {
  8. T data[N];
  9. };
  10. template <class T>
  11. struct GenericTType {
  12. T field;
  13. };
  14. struct SimpleStruct {
  15. int field;
  16. };
  17. """ .}
  18. type
  19. GenericIntType {.importcpp: "GenericIntType<'0, '1>".} [N: static[int]; T] = object
  20. data: array[N, T]
  21. GenericIntTypeAlt {.importcpp: "GenericIntType".} [N: static[int]; T] = object
  22. GenericTType {.importcpp: "GenericTType<'0>".} [T] = object
  23. field: T
  24. GenInt4 = GenericIntType[4, int]
  25. SimpleStruct {.importcpp: "SimpleStruct"} = object
  26. field: int
  27. var
  28. a = GenInt4()
  29. b = SimpleStruct()
  30. c = GenericTType[float]()
  31. d = SimpleStruct(field: 15)
  32. e = GenericTType[string](field: "test")
  33. f = GenericIntTypeAlt[4, int8]()
  34. a.data[2] = 10
  35. b.field = 5
  36. c.field = 1.2
  37. echo a.data
  38. echo b.field
  39. echo c.field
  40. echo d.field
  41. echo e.field
  42. proc plus(a, b: GenInt4): GenInt4 =
  43. for i in 0 ..< result.data.len:
  44. result.data[i] = a.data[i] + b.data[i]
  45. echo plus(a, a).data
  46. echo sizeof(f)