Why is void f(…) not allowed in C?

后端 未结 6 1717
悲&欢浪女
悲&欢浪女 2021-01-05 15:31

Why doesn\'t C allow a function with variable length argument list such as:

void f(...)
{
    // do something...
}
6条回答
  •  长情又很酷
    2021-01-05 15:38

    Playing around with it, made this nice implementation that I think some people might want to consider.

    template
    void print(T first, ...)
    {
        va_list vl;
        va_start(vl, first);
        T temp = first;
        do
        {
            cout << temp << endl;
        }
        while (temp = va_arg(vl, T));
        va_end(vl);
    }
    

    It ensures you have one variable minimum, but allows you to put them all in a loop in a clean way.

提交回复
热议问题