且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c中具有不同类型参数的可变参数函数

更新时间:2022-11-24 15:41:19

在C99中,可变参数函数使用 stdarg(3)操作(通常实现为宏,扩展为特定于编译器的神奇的东西,例如GCC内置函数).第一个参数应具有某种固定的已知类型,并通常确定如何获取其他参数.在当今的几种ABI中,可变参数函数传递不使用寄存器,因此效率不如固定arity函数调用.

In C99, variadic functions use stdarg(3) operations (generally implemented as macros expanding to compiler-specific magical stuff, e.g. GCC builtins). The first arguments should have some fixed known type, and generally determinates how the other arguments are fetched; in several today's ABIs, variadic function argument passing don't use registers so is less efficient than fixed arity function calls.

对于您来说,您***具有多个功能.为了获得灵感,sqlite具有多个打开函数.

In your case, you'll better have several functions. For inspiration sqlite has several open functions.

请注意,POSIX使用一个 last 可选参数定义了某些功能,尤其是

Notice that POSIX defines some function with one last optional argument, in particular open(2). This is probably implemented as having a function whose last argument can be not supplied.

您可以阅读 ABI 规范和

You could read the ABI specification and the calling conventions specific to your implementation.

顺便说一句,缺少函数重载(例如在C ++中)可以理解为C的优点(函数名-这是名称处理.另请参阅动态链接

BTW, lack of function overloading (like in C++) can be understood as an advantage of C (the function name -which is the only thing the linker care about, at least on Linux & Unix- determines its signature). Hence C++ practically needs name mangling. Read also about dynamic linking and dlopen