![](/img/trans.png)
[英]using variable number of arguments in a function call and delegating them to another function
[英]Pass variable number of arguments received to another function
在你说它重复之前,我已经读过: 如何将可变数量的参数从一个函数传递到另一个函数?
我有这样的功能:
void tlog_function(t_log* logger, const char* message_template, ...) {
pthread_mutex_lock(&loggerLock);
log_function(logger, message_template, ...); // What I want to do..
pthread_mutex_unlock(&loggerLock);
}
而另一个这样的功能,不是我的,我在第三方库中使用它:
void log_function(t_log* logger, const char* message_template, ...);
正如你所看到的,我想要做的只是在这个函数中添加一个互斥锁以使其成为线程安全的,我知道我可以使用va_list
但是在这种情况下我不能改变第二个函数的代码,因为它在一个内部库我只有.h文件。
那么,有没有办法实现这一目标?
如果您无法更改代码并且没有库函数的va_list
版本,您可以使用我所知道的最接近的事情是使用宏和##__VA_ARGS__
( 这是仅GCC扩展 )。
你会想要这样的东西:
#define tlog_function(logger, message_template, ...) do { \
pthread_mutex_lock(&loggerLock); \
log_function(logger, message_template, ##__VA_ARGS__); \
pthread_mutex_unlock(&loggerLock); \
} while(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.