[cfe-dev] Pointers to Overloaded C Functions

Douglas Gregor dgregor at apple.com
Mon Jan 4 12:15:05 CST 2010


On Jan 4, 2010, at 10:13 AM, Eli Friedman wrote:

> 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.

I don't know if we even want to support that kind of code... the  
"overloadable" attribute is narrowly defined to help support certain  
weird C99-isms (*cough* tgmath.h *cough*). We don't really want to  
encourage widespread use, since it is likely to never be portable.

	- Doug


More information about the cfe-dev mailing list