tempty_generic_obj.nim 1.2 KB

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