Consider the following program:
#include <iostream>
typedef void (*fptr)(...);
void foo(fptr func) {
(*func)(12);
}
void bar(int x) {
std::cout << "bar : " << x << std::endl;
}
int main() {
foo(fptr(bar));
}
This compiles, runs and prints bar : 12
on at least one compiler :) I found this in some legacy code I am supposed to maintain, and I wonder if this is safe/defined?
bar
does not match the type fptr
, so the only way to get this to work is by using an unsafe cast. I guess it depends on how the ellipsis-magic works internally, so is that defined in some way?
fptr
, since variadic functions always need at least one named argument. So you can define the type and make a pointer variable, but you can't assign it any valid value. – Myrmeco