I am facing smart pointers or something like that in some big libraries. I understand that this is how C++ deal with the lack of GC, is it like that?
How should I deal with them when facing a library that use them? Should I try to use them through Nim, or should I try to avoid them and use Nim's own mechanism?
As an example, in this article mention that OpenSceneGraph use their set of smart pointer.
I don't think you can avoid them if they are used internally in the library. You would have to wrap them and it might get ugly:
This C++ :
osg::ref_ptr<osg::Group> root = new osg::Group;
Will probably turn into this Nim:
var root: refPtr[Group] = newGroup()
Good luck!
I wrap them with: https://github.com/numforge/agent-smith/blob/a2d9251/third_party/std_cpp.nim#L22
type
CppUniquePtr* {.importcpp: "std::unique_ptr", header: "<memory>", byref.} [T] = object
And then I can sue them in Nim https://github.com/numforge/agent-smith/blob/a2d9251/third_party/ale_wrap.nim#L96-L103
type
ALEInterface* {.importcpp:"ALEInterface",
header: cppSrcPath & "ale_interface.hpp",
byref.} = object
theOSystem*{.importcpp.}: CppUniquePtr[OSystem]
theSettings*{.importcpp.}: CppUniquePtr[Settings]
romSettings*{.importcpp.}: CppUniquePtr[RomSettings]
environment*{.importcpp.}: CppUniquePtr[StellaEnvironment]
max_num_frames*: int32 ## Maximum number of frames for each episode
I understand that this is how C++ deal with the lack of GC, is it like that?
C++ smart pointers are actually very much like the way Nim ARC is implemented internally. Or the other way around, under ARC a ref is like a C++ smart pointer.
(To be pedantic, this applies to ref-counted smart pointers, like std::shared_ptr. C++ also has smart pointers that allow only one owner, std::unique_ptr. Nim doesn't have an equivalent of this; it's more like Rust's Box type.)
C++ also has smart pointers that allow only one owner, std::unique_ptr. Nim doesn't have an equivalent of this.
Owned refs will be like this: https://github.com/nim-lang/RFCs/issues/144 but for Nim v2.