tempty_generic_obj.nim 1.1 KB

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