tempty_generic_obj.nim 847 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. discard """
  2. targets: "cpp"
  3. output: '''int
  4. float'''
  5. """
  6. import typetraits
  7. # bug #4625
  8. type
  9. Vector {.importcpp: "std::vector<'0 >", header: "vector".} [T] = 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* {.importcpp: "std::vector", header: "<vector>", nodecl.} [T] = 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()