[cfe-dev] RFC: Function Overloading in C

Chris Lattner clattner at apple.com
Fri Feb 13 08:33:31 PST 2009


On Feb 13, 2009, at 2:01 AM, Sebastian Redl wrote:

> Douglas Gregor wrote:
>> Hi all,
>>
>> I've just finished the implementation of the "overloadable" function
>> attribute that introduces function overloading into C. The intent is
>> to make it easier to implement "type-generic" functions (such as  
>> those
>> in tgmath.h and altivec.h). The feature is in Clang's trunk, and is
>> documented here:
>>
>> 	http://clang.llvm.org/docs/LanguageExtensions.html#overloading-in-c
>>
>> Comments welcome!
>>
> I wonder if it's possible (and a good idea) to use the C++ linkage
> specification syntax for this, i.e.
>
> extern "C++" double tgsin(double d) { return sin(d); }
> extern "C++" float tgsin(float f) { return sinf(f); }
> extern "C++" long double tgsin(long double d) { return sinl(d); }
>
> As a C++ programmer, it looks more natural to me. Of course, I have no
> idea how this would look to a pure C programmer. Also, this syntax  
> would
> imply that we will always mangle this like C++ names.

I actually prefer an attribute, for two reasons.  1) this would  
require adding support for linkage specs to the "c parser" and "c  
sema" which is a bigger change, 2) to the user, the connection that  
this would enable overloading, but nothing else, seems strange.

As a user, I would expect to be able to write something like this as  
well:
extern "C++" template <typename t> void foo();

Attributes are "ugly", but they are very standard, and it makes it  
very clear that it enables "overloadability"

-Chris



More information about the cfe-dev mailing list