Function overloading in C

2020-02-05 12:31发布

问题:

Today, looking at the man page for open(), I've noticed this function is 'overloaded':

   int open(const char *pathname, int flags);
   int open(const char *pathname, int flags, mode_t mode);

I didn't thought it's possible on C. What's the 'trick' for achieving this ?

LATER EDIT:
So it's not really overloading, because when using varargs - you can only supply multiple arguments of the same type. So, is mode_t behind the scenes an int ?

回答1:

It's using variable arguments. Those declarations appear only in the man page, as those 2 are the only ways you should call open(). The actual C function will be declared as e.g.

int open(const char *pathname,int flags,...);

With variable arguments, the arguments don't need to be the same type. printf is the obvious example of this.

In the case of open(), the first variable argument have to be mode_t if 'flags contain the O_CREAT flag because implementation of open() expects it to be mode_t (which behind the scenes is likely an unsigned int or unsigned long - but that has nothing to do with varargs)



回答2:

C does make it possible to write function with a variable number of argument, such as printf.

With that being said, there is no reliable, cross-platform way in C to write a function that takes exactly 2 or 3 arguments; in general you must do something like

some_function(5, 6, 7, NULL);
some_function(5, 6, 8, 2, 5, NULL);

In other words, you must have a terminating "sentinal" argument. Alternatively, you could include the number of parameters somehow in an earlier parameter, such as

another_func(2, "hello", "world");
another_func(3, "goodbye", "cruel", "world");

The printf family of functions take this approach; the first format parameter contains the number of extra parameter needed; e.g. with printf("%f %f", 5.6, 7.11) you know that there must be 2 float parameters. However, this would be somewhat unsafe in a user-defined library function, since if you said my_printf("%s %f %f %f %s", 5.6) then you could get segfaults or worse. Fortunately, most C compilers will check your calls to printf at compile time to avoid this kind of issue.

In the case of open, the function is declared as having variable arguments, and the third parameter is only checked if O_CREAT is set. So this is how it "safely" determines whether a third argument is present. I put "safely" in quotes because technically there's no way for open to know at runtime how many parameters were actually passed. For example, the following calls would compile without any errors or warnings:

open("foo.txt", 5, "not an integer", 7);    // extra and invalid parameters
open("bar.txt", O_CREAT);                   // third parameter is missing


回答3:

"mode must be specified when O_CREAT is in the flags, and is ignored otherwise."

extern int open (__const char *__file, int __oflag, ...)

It uses varargs and only loads the mode variable argument if __oflag contains O_CREAT.



回答4:

very short answer - varargs



回答5:

you can fake it using variable arguments list with ...

int function(int x, ...);