I have to use IAR compiller in embedded application (it does not have namespaces, exceptions, multiple/virtual inheritance, templates are bit limited and only C++03 is supported).
I can't use parameter pack so I tried to create member function with variadic parameter.
I know variadic parameters are generally unsafe. But is safe to use this
pointer in va_start
macro?
If I use ordinary variadic function it would need a dummy parameter before ...
to be able to access remaining parameters. I know variadic macro would not need parameter before ...
but I would prefer not to use it.
If I use member function it has hidden this
parameter before ...
so I tried it.:
struct VariadicTestBase{
virtual void DO(...)=0;
};
struct VariadicTest: public VariadicTestBase{
virtual void DO(...){
va_list args;
va_start(args, this);
vprintf("%d%d%d\n",args);
va_end(args);
}
};
//Now I can do
VariadicTestBase *tst = new VariadicTest;
tst->DO(1,2,3);
tst->DO(1,2,3);
prints 123 as expected. But I am not sure if it is not just some random/undefined behavior. I know tst->DO(1,2);
would crash just like normal prinf would. I do not mind it.