When designing a C++ library, I read it is bad practice to include standard library containers like std::vector
in the public interface (see e.g. Implications of using std::vector in a dll exported function).
What if I want to expose a function that takes or returns a list of objects? I could use a simple array, but then I would have to add a count
parameter, which makes the interface more cumbersome and less safe. Also it wouldn't help much if I wanted to use a map
, for example. I guess libraries like Qt define their own containers which are safe to export, but I'd rather not add Qt as a dependency, and I don't want to roll my own containers.
What's the best practice to deal with containers in the library interface? Is there maybe a tiny container implementation (preferably just one or two files I can drop in, with a permissive license) that I can use as "glue"? Or is there even a way to make std::vector
etc. safe across .DLL/.so boundaries and with different compilers?