I want to forward calls to a library which has a variadic method. The simplest example I could come up with to replicate the problem is this:
void Bar(int useless, ...)
{
//Does something
}
template<typename... Args>
void Foo(int useless, Args... args)
{
Bar(useless, args...);
}
As you can see, I have had a go. However, even though this compiles, it seems to cause the stack to have a wobbler and I see errors on application exit. I can understand from the compilers point of view that this solution is problematic.
I am unsure how to get this working, or whether it is even possible to get this working. I have seen some people suggest using the "indices trick" in similar situations but I have not been able to get it working on this particular occasion.
Any help appreciated!
The approach you are taking is very sane, safe and sound by itself. For example, the below program is using variadic template arguments and forwards them to
std::printf()
, which is a C-style function that accepts variable number of arguments:The problem with undefined behavior and possibly stack corruption is caused by something else. In C++, this is most likely caused by passing non-POD types though C's variable argument list (i.e. passing
std::string
toprintf()
can do this).Unfortunately, compiler extensions like "format attribute" won't help here. But compiler might warn you about non-POD types. For example, call
my_printf()
withstd::string
and you will be rewarded with a warning similar to this:Of course, there could be something else going on that compiler won't be able to catch for you. Unfortunately, there is no better way than to debug your program and see what exactly is causing it to crash.
Since you are entering dark waters of C and unsafe arguments passing, debugger is your friend here, and I'd also recommend you use Valgrind - it is very helpful catching things like that.
Hope it helps. Good Luck!