Casting pointer to ellipsis function

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, starts and prints bar : 12 for at least one compiler :) I found this in some outdated code that I have to support, and I wonder if it is safe / defined?

bar does not match the fptr type, so the only way to get this to work is to use an unsafe listing. I think it depends on how the ellipsis magic inside works, is it somehow determined?

+7
source share
1 answer

What the code does is its undefined behavior. If his work is just by chance, there is no guarantee that it should work. The only thing that can be safely done with the casted function pointer is to return it back to its original type.

+9
source

All Articles