Function Signature Changes for Directly Compatible Interfaces

Is there a common practice of extending API C while maintaining compatibility with it?

Suppose I want to provide the foo function in the c library:

foo(int value);

Now in a later version, I would like to extend foo to allow another parameter.

foo(int value, const char *description_may_be_NULL);

To provide advanced compatibility, I would name the new one differently, for example. foo2.

It would be wise to provide a macro to:

#define MYLIB_API 2
#include <mylib.h>
/* the header will #define foo foo2 */

This would avoid the use of bad names in practice.

Are there any common methods for handling conflicting advanced compatibility and elegant code for future versions? Any examples of how popular C APIs have handled in the past will be appreciated.

: , : foo(int) , varargs/struct API. , , . , -, .

+4
3

...

typedef struct
{
    int value;
} typeStructure;

void foo(const typeStructure * const pTypeStructure);

typeStructure, foo .

+3

:

void foo(int arg, ...);

. (, , arg ), va_arg, .

+2

Use foo();. no argument inside the brackets means an indefinite number of arguments, and you can call it with any number of parameters.

Another way is to use va_argsa variable argument function, such as printf(), or a variable macro that calls yourfoo

#define FOO(...) foo(__VA_ARGS_)

0
source

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


All Articles