I want to call an extern "C"
function e.g. f1(int a, float f, double d, void* ptr)
using an forward declaration with actual parameters but in the actual implementation I would like to to use va_list
and friends to pop
the args. Let's imagine for a second that I have a valid usecase for it, is it allowed?
main.cpp
extern "C" void f(int anchor, int a, float f, double d, void* ptr, char c);
int main(int, char*)
{
f(0, 42, 1.08f, 3.14, reinterpret_cast<void*>(0xcafebabe), 'c');
return 0;
}
impl.cpp
#include <cstdarg>
#include <iostream>
#include <iomanip>
using namespace std;
void f(int anchor, ...)
{
va_list args;
va_start(args, anchor);
int a = va_arg(args, int);
float f = va_arg(args, float);
double d = va_arg(args, double);
void* ptr = va_arg(args, void*);
char c = va_arg(args, char);
cout << a << ' '
<< f << ' '
<< d << ' '
<< hex << (std::ptrdiff_t)ptr << ' '
<< (int)c << endl;
va_end(args);
}
The code runs and prints the correct values on MSVC 2015 atleast, the question now is: is it guaranteed to work, if not: will it probably work on the most important platforms and compilers anyway?