tempty_generic_obj.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. discard """
  2. targets: "cpp"
  3. output: '''int
  4. float'''
  5. """
  6. import typetraits
  7. # bug #4625
  8. type
  9. Vector[T] {.importcpp: "std::vector<'0 >", header: "vector".} = object
  10. proc initVector[T](): Vector[T] {.importcpp: "'0(@)", header: "vector", constructor.}
  11. proc doSomething[T](v: var Vector[T]) =
  12. echo T.name
  13. var v = initVector[int]()
  14. v.doSomething()
  15. var vf = initVector[float]()
  16. vf.doSomething() # Nim uses doSomething[int] here in C++
  17. # Alternative definition:
  18. # https://github.com/nim-lang/Nim/issues/7653
  19. type VectorAlt*[T] {.importcpp: "std::vector", header: "<vector>", nodecl.} = object
  20. proc mkVector*[T]: VectorAlt[T] {.importcpp: "std::vector<'*0>()", header: "<vector>", constructor, nodecl.}
  21. proc foo(): VectorAlt[cint] =
  22. mkVector[cint]()
  23. proc bar(): VectorAlt[cstring] =
  24. mkVector[cstring]()
  25. var x = foo()
  26. var y = bar()
  27. proc init[T; Self: Vector[T]](_: typedesc[Self], n: csize_t): Vector[T]
  28. {.importcpp: "std::vector<'*0>(@)", header: "<vector>", constructor, nodecl.}
  29. proc size[T](x: Vector[T]): csize_t
  30. {.importcpp: "#.size()", header: "<vector>", nodecl.}
  31. var z = Vector[int16].init(32)
  32. assert z.size == 32