Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is overloading not possible in C? [closed]

I know that overloading is not possible in C, and I want to know: why is overloading functions within the class and outside the class treated the same in C++?

Consider this case in C++ where the functions are declared outside the class:

foo()
foo(char c)
foo(int a, int b)

If C++ treats each function header as unique, why can't C do as well?

I think these might be the reason:

  • Function overloading was introduced in C++, so it is not available in C.
  • Polymorphism is an OOP concept, but C is not object-oriented.

Are there any reasons for the unavailability of function-overloading in C?

like image 911
Venkatesh Avatar asked Dec 14 '14 12:12

Venkatesh


2 Answers

Perhaps the most important reason is the lack of prior art. There are very few examples of features added to C by the standard committee which have neither been popular extensions before nor are necessary to write strictly conforming code.

There is some vaguely defined "spirit of C", the C99 rationale (V5.10) reads (page 3, emph. mine):

Some of the facets of the spirit of C can be summarized in phrases like:

  • Trust the programmer.
  • Don't prevent the programmer from doing what needs to be done.
  • Keep the language small and simple.
  • Provide only one way to do an operation.
  • Make it fast, even if it is not guaranteed to be portable.

Further, C tries hard to stay backwards compatible with older revisions; old-style declarations, for example, have been marked obsolescent in C89 but are still part of C11. Ibid, page 2:

Existing code is important, existing implementations are not. A large body of C code exists of considerable commercial value. Every attempt has been made to ensure that the bulk of this code will be acceptable to any implementation conforming to the Standard. The C89 Committee did not want to force most programmers to modify their C programs just to have them accepted by a conforming translator.

Some differences in C and C++ are at least partly caused by function overloading, for example the type of character constants:

int foo(int);
int foo(char);
...
    foo('x');

In C++, this calls foo(char), but in C, the type of 'x' is int, so either the result would be rather surprising, or 'x' needed to be of type char, possibly breaking existing code. Further, you probably want some promotions where they make sense, e.g. if in the last example, the second declaration of foo wasn't given, 'x' would be promoted to int and foo(int) was called. Such rules can become complex in detail (should void * be implicitly converted for function calls?). (Not a hard number, but the chapter about function overloading in the C++ standard (chapter 13) in the n3797 draft covers about 30 pages, chapter ibid. 5.2.2 about function calls is considerably longer than the corresponding C standard part.)

Pretty much every feature of C is necessary for a minimal language (well, modulo historic legacy), there is very little syntactic sugar; function overloading could be considered such (you could name your functions foo_int and foo_char etc and call the correct one explicitly).

The reasons you suggested, are circular (and thus aren't applicable): C did adopt some C++ features (e.g. function prototypes); and function overloading was introduced in C++ because C lacked it (you cannot say "It isn't part of C, because it's part of C++; and it's part of C++, because it isn't part of C"). Same goes for the second suggestion about C and OOP.

What I personally like about C is that it maps rather closely to the machine. It's often easy to see how assembler output generated from C code relates to it. The symbol names are unmangled and can be easily identified. The language is kept simple and minimal. Frankly, I don't understand what people are after when they want to see certain C++ features incorporated into C: We have C++, a language which offers those things, with the ability to write platform-specific, efficient code; why not just use it?

C11 introduced _Generic, which may be of interest for you.

like image 153
mafso Avatar answered Sep 25 '22 21:09

mafso


You may find some interesting point in C99 rationale 7.22/25 Type-generic math <tgmath.h> :

The ability to overload on integer as well as floating types would have been useful for some functions, for example copysign. Overloading with different numbers of arguments would have allowed reusing names, for example remainder for remquo. However, these facilities would have complicated the specification; and their natural consistent use, such as for a floating abs or a two-argument atan, would have introduced further inconsistencies with C89 for insufficient benefit.

Also C11 Standard introduced type _Generic macros, that allows to some sort of overloading, in sense that different types may be handled as function-like macros arguments.

like image 42
Grzegorz Szpetkowski Avatar answered Sep 23 '22 21:09

Grzegorz Szpetkowski