Create va_list dynamically

后端 未结 7 734
一个人的身影
一个人的身影 2020-12-06 01:08

I have a function

void foo(int cnt, va_list ap);

I need to use it, but requirement is quite strict, number of va_list vary an

7条回答
  •  情歌与酒
    2020-12-06 01:22

    If the number of elements in the list is limited, I would go for manual dispatch depending on the number of elements.

    void call_foo(int count, ...) {
        va_list args;
        va_start(args, count);
        foo(count, args);
        va_end(args);
    }
    
    switch (contacts.count()) {
        case 0: return call_foo(contacts.count());
        case 1: return call_foo(contacts.count(),
                                contacts.at(0)->getName());
        case 2: return call_foo(contacts.count(),
                                contacts.at(0)->getName(),
                                contacts.at(1)->getName());
        case 3: return call_foo(contacts.count(),
                                contacts.at(0)->getName(),
                                contacts.at(1)->getName(),
                                contacts.at(2)->getName());
        default: /* ERROR HERE, ADD MORE CASES */ return call_foo(0);
    }
    

提交回复
热议问题