Why is overload impossible in C?

I know that overloading is not possible in C, and I want to know: why are overload functions inside the class and outside the class handled the same in C ++?

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

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

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

I think this may 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 lack of functional overload in C?

+7
c ++ c oop overloading
source share
3 answers

Perhaps the most important reason is the lack of prior art. There are very few examples of functions added to the C standard committee that were not popular extensions before and are not needed to write strictly appropriate code.

There is some vague "spirit C", an explanation of C99 (V5.10) reads (p. 3, above):

Some of the aspects of C can be summarized in phrases such as:

  • Trust the programmer.
  • Do not let the programmer do what needs to be done.
  • Keep your tongue small and simple.
  • Provide only one way to perform the operation.
  • Do it quickly, even if it is not guaranteed to carry over.

In addition, C tries to remain backward compatible with older versions; For example, old-style ads were marked obsolete in C89, but are still part of C11. Ibid., P. 2:

Existing code is important; there are no existing implementations. There is a large array of C code of significant commercial value. Every attempt has been made to ensure that the bulk of this code is acceptable for any implementation that conforms to the standard. The C89 committee did not want to force most programmers to modify their C programs only to be accepted with the help of an appropriate translator.

Some differences in C and C ++ are at least partially 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 'x' is int , so either the result will be quite unexpected, or 'x' should be of type char , possibly breaking existing code. In addition, you probably need some promotions where they make sense, for example. if the second declaration foo was not declared in the last example, then 'x' would be upgraded to int and foo(int) . Such rules can become complex in detail (if void * implicitly converted to function calls?). (Not a hard number, but the chapter on function overloading in the C ++ standard (chapter 13) in the n3797 project covers about 30 pages, the ibid 5.2.2 chapter on function calls is much longer than the corresponding standard part C.)

Virtually every C feature is needed for a minimal language (well, modulo historical heritage), very little syntactic sugar; such an overload of functions may be considered (you could name your functions foo_int and foo_char , etc. and call them correctly).

The reasons you suggested are circular (and therefore not applicable): C used some C ++ functions (for example, function prototypes); and function overloading was introduced in C ++ because C was missing it (you cannot say: "This is not part of C because it is part of C ++, but part of C ++ because it is not part of C" ) The same goes for the second sentence on C and OOP.

What I personally like about C is that it pretty closely matches the machine. It is often easy to see how the assembler assembly generated by the C code relates to it. The names of the characters are unrelated and can be easily identified. The language remains simple and minimal. Honestly, I don’t understand what people after, when they want to see some of the C ++ functions included in C: we have C ++, a language that offers these things, with the ability to write efficient code for a specific platform; why not just use it?

C11 introduced _Generic , which may interest you.

+8
source share

You can find some interesting point in Justification C99 7.22 / 25 Typical math <tgmath.h> :

The ability to overload both integer and floating types was useful for some functions, for example copysign . overloading with a different number of arguments would allow the reuse of names, for example remainder for remquo . However, these objects are a complex specification; and their natural sequential use, such as for floating abs or two-factor atan, would introduce further inconsistencies with C89 for insufficient benefit.

Also C11 The standardly introduced type is _Generic macro, which allows some overloading in the sense that various types can be processed as arguments to function type macros.

+8
source share

I would say that the first and most important reason C does not have function overloads is that C does not change function names:

Each function in C is converted to a character with the same name, and in C ++ the name becomes distorted.

Example:

Take your functions:

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

Character names that are garbled by C ++ code will look like this:

 void foo() = _Z3foov void foo(char c) = _Z3fooc void foo(int a,int b) = _Z3fooii 

In C code, this translates to:

 void foo() = foo 

Bottom line - C ++ allows you to manipulate names, so the compiler "changes" the names of functions and all references to three different names

C does not allow changing the name according to the latest standard (and I doubt it will ever change), so such a solution is simply impossible.

+3
source share

All Articles