#include <iostream>
#include <cstdio>
#include <stdarg.h>
template<typename... Args>
void log(Args&&... args)
{
    printf(std::forward<Args>(args)...);
}
#define macro_printf(...) printf(__VA_ARGS__)
void my_printf(const char *fmt,...)
{
    // char buf[2048] = {0};
    va_list vaList; //定义一个具有va_list型的变量,这个变量是指向参数的指针。
    va_start(vaList, fmt);//第一个参数指向可变列表的地址,地址自动增加,第二个参数位固定值
    // vsprintf(buf,fmt,vaList);
    vprintf(fmt,vaList);
}
int main(int argc,char **argv)
{
    log("1: 123 %d %f %s\n",456,789.123,"10,11,12");
    macro_printf("2: 123 %f\n",456.789);
    my_printf("3: this in function .... %s\n","hello world!");
    return 0;
}
root@jeason:~/work/dynamicParams# g++ 1.cpp && ./a.out
1: 123 456 789.123000 10,11,12
2: 123 456.789000
3: this in function .... hello world!

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐