Safely use containers in C++ library interface

前端 未结 4 1055
予麋鹿
予麋鹿 2021-02-02 00:28

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 us

4条回答
  •  情深已故
    2021-02-02 00:43

    Actually this is not only true for STL containers but applies to pretty much any C++ type (in particular also all other standard library types).

    Since the ABI is not standardized you can run into all kinds of trouble. Usually you have to provide separate binaries for each supported compiler version to make it work. The only way to get a truly portable DLL is to stick with a plain C interface. This usually leads to something like COM, since you have to ensure that all allocations and matching deallocations happen in the same module and that no details of the actual object layout are exposed to the user.

提交回复
热议问题