Hello,
Has anyone written a Nim wrapper for the C++ std::vector library (even incomplete)?
I started putting it together using bits and pieces I found from googling, but as I don't know C++, I reached a roadblock very soon when I tried to map the std::vector::iterator and associated procs like begin and end.
Found this: https://github.com/BigEpsilon/nim-cppstl/blob/master/src/cppstl/vector.nim
Have no idea if it works.
Fragments: https://github.com/fragcolor-xyz/fragments#cpp (and see actual usage here: https://github.com/fragcolor-xyz/nimtorch/blob/v0.2.3/torch/torch_cpp.nim)
Or roll your own, my way: https://github.com/numforge/agent-smith/blob/a2d9251e/third_party/std_cpp.nim#L23-L31
@deech
Thanks. That will serve as a useful reference. I looked through it, but it doesn't give an example on how to use the vector::iterator type.
Just by fluke, I replaced the begin and end proc types to be ptr T instead of the iterator, and it returns the begin and end pointers.. though I don't know yet how to use create an iterator in Nim for the Vector.
My attempt so far: https://play.nim-lang.org/#ix=271v
@mratsim: Thank you for sharing the "roll your own"; seems like I started off the same path (see above link). I am though stuck at how to use the VectorIterator.
Thanks for all the help.
Also, this construct:
type
Vector* {.importcpp: "std::vector", header: "vector".} [T] = object
gives this warning:
Warning: pragma before generic parameter list is deprecated [Deprecated]
What is the right way to import that type so that I do not get that warning?
I made some progress! @mratsim, the [] proc imports from your snippet proved very useful. I can finally loop through the Vector values.
The only puzzle is.. how do I use the VectorIterator imported from the vector lib?
My updated wrapper: https://play.nim-lang.org/#ix=271H