Showing posts from January, 2021

Designing a C interface for complex types

  Let’s say we are designing an interface to pass a complex type between C++ modules. If there is any possibility of the two modules being built by different compilers, different versions of the same compiler, or for different platforms, we’ll need to use C functions & structures for the interface.  The same is true of C++ to another language. As an example, let’s say our module houses a lookup we want to share: If we wanted to make this lookup available to another module, due to reasons previously stated, we can’t pass our nice C++ types. So, we would need something like this:     So, the implementation of the external-facing function could look something like this:       The code in the caller would look something like this     I don’t love this approach. To be honest, I’ve never liked returning values via parameter. Especially, in functions with several parameters, I think using return values is much clearer to the reader. In this case, it also puts a numbe