[cfe-dev] Pointers to Overloaded C Functions
Eli Friedman
eli.friedman at gmail.com
Mon Jan 4 10:13:26 PST 2010
On Mon, Jan 4, 2010 at 9:53 AM, George King <gwk.lists at gmail.com> wrote:
> I am experimenting with clang's overloaded C functions, and I can't figure out how to get a pointer to an overloaded function.
>
> test program:
>
> #define OL __attribute__((overloadable))
>
> void OL a(int x) { printf("i: %d\n", x); }
> void OL a(float x) { printf("f: %f\n", x); }
>
> int main(int argc, char** argv)
> {
> a(1);
> a(1.0f);
>
> void(*a_i_ptr)(int) = a;
> void(*a_f_ptr)(float) = a;
>
> return 0;
> }
>
>
> compiler output:
>
>> clang test_ol.c
> test_ol.c:25:11: error: incompatible type initializing '<overloaded function type>', expected
> 'void (*)(int)'
> void(*a_i_ptr)(int) = a;
> ^ ~
> test_ol.c:26:11: error: incompatible type initializing '<overloaded function type>', expected
> 'void (*)(float)'
> void(*a_f_ptr)(float) = a;
> ^ ~
> 2 diagnostics generated.
>
>
> If I change the name of one of the functions, then it compiles; clang appears well aware of the syntactic ambiguity. Is there a more specific syntax to specify which function I'm referring to?
>
> Something like "void(*a_i_ptr)(int) = a(int);" makes sense to me, but I imagine that it's a stretch as far as extending the language goes.
Umm, interesting... I think it's just some missing logic in the code
for assignments in C. I don't think anyone considered that someone
might write code like that. You have the syntax right; the given code
works properly if clang is in C++ mode.
-Eli
More information about the cfe-dev
mailing list