跳至主要内容

博文

目前显示的是 四月, 2016的博文

Implement function overloading in c

Origin When I learn the system call of linux, I saw the functions like this: int open( const char *pathname, int flags); int open( const char *pathname, int flags, mode_t mode); And this: int fcntl ( int fd, int cmd); int fcntl ( int fd, int cmd, long arg); int fcntl ( int fd, int cmd, struct flock *lock ); They seems normal, right? But c has no support for function overloading, how this system call implemented? Analysis In order to find out the implementation, we first dive into the head of those functions. We are going to take the fcntl as example. The header of fcntl() is called fcntl.h , which is located under /usr/include/ with other c header files. $ cat fcntl.h | grep 'fcntl' extern int fcntl (int __fd, int __cmd, ... ); # include <bits/fcntl2.h> Upon seeing this, we understand that they are implemented by variadic function. But with a variadic function, we can’t tell how many arguments that it has, so it’s not t