Using std :: function for an API (across module boundaries)

I'm pretty sure that I know the answer to this question (thinking no), but is it possible to safely accept / return std :: function by value in the API (across module boundaries)?

I think no, because I don’t think there is any guarantee that the implementation of one std :: function provider is compatible with others. Is that the case?

If the answer is not the same as I suspect, how do you all deal with this? I may have to resort to implementing my own or just avoid things like std :: function, together (for example: working with function pointers or functions). :-( I found myself doing this in many cases before (inventing a lot C ++ standard library and, unfortunately, even creating our own STL-compatible vector type with support for both ctor and fill ctor, custom allocators, etc. was certainly not funny, and I doubt it was as good as standard implementations) simply because you can dynamically and link to a library that has, say, an implementation of MSDV 2010 std :: function from a binary file written in mingw, for example

An alternative, of course, is not to use these C ++ features in general in our API and use, say, the C interface, but for us it will be quite expensive, since we use our API as a central API for development both internally and for third-party developers.

+4
source share
1 answer

You can do this if everyone, playing by the same rules, and your standard library are dynamically linked. If third-party companies know that they must use a specific compiler and a specific version of this compiler with certain specific assembly flags, then there is no problem.

Even if you are writing your own shell, they should use a specific version of that shell, although it is obvious that it is a little easier to provide.

But really, what is the price you pay for trying to interact through the DLL: each should be on the same page, otherwise it will not work. Or you should stick with basic types or home-driven interfaces.

+6
source

Source: https://habr.com/ru/post/1398496/


All Articles